package com.hzs.system.home.controller.manage;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.home.HoMemoirHonor;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.home.service.IHoMemoirHonorService;
import com.hzs.system.home.vo.HoMemoirHonorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 大事记-资源荣誉 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-12-07
 */
@RestController
@RequestMapping("/manage/ho-memoir-honor")
public class HoMemoirHonorController extends BaseController {

    @Autowired
    private IHoMemoirHonorService memoirHonorService;

    /**
     * @description: 查询大事记-资质荣誉列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(HoMemoirHonorVO hoMemoirHonorVO) {
        startPage();
        QueryWrapper<HoMemoirHonor> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("SORT");
        if(StringUtils.isNotNull(hoMemoirHonorVO.getStartDate())
                &&StringUtils.isNotNull(hoMemoirHonorVO.getEndDate())){
            queryWrapper.between("CREATION_TIME", hoMemoirHonorVO.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,hoMemoirHonorVO.getEndDate()));
        }
        List<HoMemoirHonor> list = memoirHonorService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增大事记-资质荣誉
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoMemoirHonor memoirHonor) {

        if(StringUtils.isEmpty(memoirHonor.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        QueryWrapper<HoMemoirHonor> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("SORT");
        List<HoMemoirHonor> list = memoirHonorService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            memoirHonor.setSort(list.get(0).getSort()+1);
        }else{
            memoirHonor.setSort(1);
        }
        memoirHonor.setPkCreator(SecurityUtils.getUserId());
        memoirHonor.setCreationTime(new Date());
        return toAjax(memoirHonorService.save(memoirHonor));
    }


    /**
     * @description: 修改大事记-资质荣誉
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoMemoirHonor memoirHonor) {
        if(StringUtils.isNull(memoirHonor.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        if(StringUtils.isEmpty(memoirHonor.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }

        memoirHonor.setPkModified(SecurityUtils.getUserId());
        memoirHonor.setModifiedTime(new Date());
        return toAjax(memoirHonorService.updateById(memoirHonor));
    }

    /**
     * @description: 修改为置顶
     * @author: zhang jing
     * @date: 2022/12/7 11:55
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/updateTopping")
    public AjaxResult updateTopping(@RequestBody HoMemoirHonor memoirHonor) {
        if(StringUtils.isNull(memoirHonor.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }

        QueryWrapper<HoMemoirHonor> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("PK_ID",memoirHonor.getPkId());
        HoMemoirHonor topping =memoirHonorService.getOne(queryWrapper2);
        if(StringUtils.isNull(topping)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirHonor> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirHonor> list = memoirHonorService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirHonor toppingOld =list.get(0);
            memoirHonor.setSort(toppingOld.getSort());
            toppingOld.setSort(topping.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirHonorService.updateById(toppingOld);
        }else{
            memoirHonor.setSort(1);
        }
        memoirHonor.setPkModified(SecurityUtils.getUserId());
        memoirHonor.setModifiedTime(new Date());
        return toAjax(memoirHonorService.updateById(memoirHonor));
    }

    /**
     * @description: 上移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/moveUp")
    public AjaxResult moveUp(@RequestBody HoMemoirHonor memoirHonor) {
        if(StringUtils.isNull(memoirHonor.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoMemoirHonor memoirHonorNew =memoirHonorService.getById(memoirHonor);
        if(StringUtils.isNull(memoirHonorNew)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirHonor> queryWrapper = new QueryWrapper();
        queryWrapper.lt("SORT",memoirHonorNew.getSort());
        queryWrapper.orderByDesc("SORT");
        List<HoMemoirHonor> list = memoirHonorService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirHonor toppingOld =list.get(0);
            memoirHonor.setSort(toppingOld.getSort());
            toppingOld.setSort(memoirHonorNew.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirHonorService.updateById(toppingOld);
        }else{
            memoirHonor.setSort(1);
        }
        memoirHonor.setPkModified(SecurityUtils.getUserId());
        memoirHonor.setModifiedTime(new Date());
        return toAjax(memoirHonorService.updateById(memoirHonor));
    }

    /**
     * @description: 下移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [memoirHonor]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/moveDown")
    public AjaxResult moveDown(@RequestBody HoMemoirHonor memoirHonor) {
        if(StringUtils.isNull(memoirHonor.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoMemoirHonor memoirHonorNew =memoirHonorService.getById(memoirHonor);
        if(StringUtils.isNull(memoirHonorNew)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirHonor> queryWrapper = new QueryWrapper();
        queryWrapper.gt("SORT",memoirHonorNew.getSort());
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirHonor> list = memoirHonorService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirHonor toppingOld =list.get(0);
            memoirHonor.setSort(toppingOld.getSort());
            toppingOld.setSort(memoirHonorNew.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirHonorService.updateById(toppingOld);
        }else{
            memoirHonor.setSort(1);
        }
        memoirHonor.setPkModified(SecurityUtils.getUserId());
        memoirHonor.setModifiedTime(new Date());
        return toAjax(memoirHonorService.updateById(memoirHonor));
    }


    /**
     * @description: 查询单条个大事记-资质荣誉
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        HoMemoirHonor memoirHonor = memoirHonorService.getById(pkId);
        return AjaxResult.success(memoirHonor);
    }


    /**
     * @description: 删除大事记-资质荣誉
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoMemoirHonor> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(memoirHonorService.update(updateWrapper));
    }

}
