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


import cn.hutool.core.bean.BeanUtil;
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.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.utils.poi.ExcelUtil;
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.ext.HoEnterpriseMissionExt;
import com.hzs.common.domain.system.ext.HoMemoirProcessExt;
import com.hzs.common.domain.system.home.HoEnterpriseMission;
import com.hzs.common.domain.system.home.HoMemoirProcess;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.home.service.IHoMemoirProcessService;
import com.hzs.system.home.vo.HoEnterpriseNewsVO;
import com.hzs.system.home.vo.HoMemoirProcessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 大事记-发展历程 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/manage/ho-memoir-process")
public class HoMemoirProcessController extends BaseController {

    @Autowired
    private IHoMemoirProcessService memoirProcessExtService;

    /**
     * @description: 查询大事记-发展历程列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(HoMemoirProcessExt memoirProcessExt) {
        startPage();
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(memoirProcessExt.getTitleName())){
            queryWrapper.like("TITLE_NAME",memoirProcessExt.getTitleName());
        }
        if(StringUtils.isNotNull(memoirProcessExt.getStartDate())
                &&StringUtils.isNotNull(memoirProcessExt.getEndDate())){
            queryWrapper.between("CREATION_TIME", memoirProcessExt.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,memoirProcessExt.getEndDate()));
        }
        if(StringUtils.isNotNull(memoirProcessExt.getStartEffectDate())
                &&StringUtils.isNotNull(memoirProcessExt.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", memoirProcessExt.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,memoirProcessExt.getEndEffectDate()));
        }
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/7/19 15:34
     * @param: [response, enterpriseMission]
     * @return: void
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoMemoirProcessExt memoirProcessExt) {
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(memoirProcessExt.getTitleName())){
            queryWrapper.like("TITLE_NAME",memoirProcessExt.getTitleName());
        }
        if(StringUtils.isNotNull(memoirProcessExt.getStartEffectDate())
                &&StringUtils.isNotNull(memoirProcessExt.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", memoirProcessExt.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,memoirProcessExt.getEndEffectDate()));
        }
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirProcessVO> newlist=new ArrayList<>();
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        list.forEach(item -> newlist.add(BeanUtil.copyProperties(item, HoMemoirProcessVO.class)));
        ExcelUtil<HoMemoirProcessVO> util = new ExcelUtil<>(HoMemoirProcessVO.class);
        util.exportExcel(response, newlist, "发展历程");
    }


    /**
     * @description: 新增大事记-发展历程
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoMemoirProcess memoirProcessExt) {
        if(StringUtils.isEmpty(memoirProcessExt.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(memoirProcessExt.getSetupTime())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_SETUP_TIME_NOT_NULL));
        }

//        if(StringUtils.isNull(memoirProcessExt.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(memoirProcessExt.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("LANGUAGE_TYPE",memoirProcessExt.getLanguageType());
        queryWrapper.orderByDesc("SORT");
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            memoirProcessExt.setSort(list.get(0).getSort()+1);
        }else{
            memoirProcessExt.setSort(1);
        }
        memoirProcessExt.setPkCreator(SecurityUtils.getUserId());
        memoirProcessExt.setCreationTime(new Date());
        return toAjax(memoirProcessExtService.save(memoirProcessExt));
    }


    /**
     * @description: 修改大事记-发展历程
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoMemoirProcess memoirProcessExt) {
        if(StringUtils.isNull(memoirProcessExt.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        if(StringUtils.isEmpty(memoirProcessExt.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(memoirProcessExt.getSetupTime())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_SETUP_TIME_NOT_NULL));
        }

//        if(StringUtils.isNull(memoirProcessExt.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(memoirProcessExt.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        memoirProcessExt.setPkModified(SecurityUtils.getUserId());
        memoirProcessExt.setModifiedTime(new Date());
        return toAjax(memoirProcessExtService.updateById(memoirProcessExt));
    }

    /**
     * @description: 修改为置顶
     * @author: zhang jing
     * @date: 2022/12/7 11:55
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.TOP)
    @PostMapping("/updateTopping")
    public AjaxResult updateTopping(@RequestBody HoMemoirProcess memoirProcessExt) {
        if(StringUtils.isNull(memoirProcessExt.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        QueryWrapper<HoMemoirProcess> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("PK_ID",memoirProcessExt.getPkId());
        HoMemoirProcess topping =memoirProcessExtService.getOne(queryWrapper2);
        if(StringUtils.isNull(topping)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("LANGUAGE_TYPE",topping.getLanguageType());
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirProcess toppingOld =list.get(0);
            memoirProcessExt.setSort(toppingOld.getSort());
            toppingOld.setSort(topping.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirProcessExtService.updateById(toppingOld);
        }else{
            memoirProcessExt.setSort(1);
        }
        memoirProcessExt.setPkModified(SecurityUtils.getUserId());
        memoirProcessExt.setModifiedTime(new Date());
        return toAjax(memoirProcessExtService.updateById(memoirProcessExt));
    }

    /**
     * @description: 上移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.UP_MOVE)
    @PostMapping("/moveUp")
    public AjaxResult moveUp(@RequestBody HoMemoirProcess memoirProcessExt) {
        if(StringUtils.isNull(memoirProcessExt.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoMemoirProcess memoirProcess =memoirProcessExtService.getById(memoirProcessExt);
        if(StringUtils.isNull(memoirProcess)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
        queryWrapper.lt("SORT",memoirProcess.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",memoirProcess.getLanguageType());
        queryWrapper.orderByDesc("SORT");
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirProcess toppingOld =list.get(0);
            memoirProcessExt.setSort(toppingOld.getSort());
            toppingOld.setSort(memoirProcess.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirProcessExtService.updateById(toppingOld);
        }else{
            memoirProcessExt.setSort(1);
        }
        memoirProcessExt.setPkModified(SecurityUtils.getUserId());
        memoirProcessExt.setModifiedTime(new Date());
        return toAjax(memoirProcessExtService.updateById(memoirProcessExt));
    }

    /**
     * @description: 下移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [memoirProcessExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.DOWN_MOVE)
    @PostMapping("/moveDown")
    public AjaxResult moveDown(@RequestBody HoMemoirProcess memoirProcessExt) {
        if(StringUtils.isNull(memoirProcessExt.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoMemoirProcess memoirProcess =memoirProcessExtService.getById(memoirProcessExt);
        if(StringUtils.isNull(memoirProcess)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoMemoirProcess> queryWrapper = new QueryWrapper();
        queryWrapper.gt("SORT",memoirProcess.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",memoirProcess.getLanguageType());
        queryWrapper.orderByAsc("SORT");
        List<HoMemoirProcess> list = memoirProcessExtService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoMemoirProcess toppingOld =list.get(0);
            memoirProcessExt.setSort(toppingOld.getSort());
            toppingOld.setSort(memoirProcess.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            memoirProcessExtService.updateById(toppingOld);
        }else{
            memoirProcessExt.setSort(1);
        }
        memoirProcessExt.setPkModified(SecurityUtils.getUserId());
        memoirProcessExt.setModifiedTime(new Date());
        return toAjax(memoirProcessExtService.updateById(memoirProcessExt));
    }


    /**
     * @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) {
        HoMemoirProcess memoirProcessExt = memoirProcessExtService.getById(pkId);
        return AjaxResult.success(memoirProcessExt);
    }


    /**
     * @description: 删除大事记-发展历程
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DEVELOP,business = EOperationBusiness.DEVELOP, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoMemoirProcess> 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(memoirProcessExtService.update(updateWrapper));
    }
}
