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


import cn.hutool.core.bean.BeanUtil;
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.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.home.HoEnterpriseMission;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.home.service.IHoEnterpriseMissionService;
import com.hzs.system.home.vo.HoEnterpriseMissionVO;
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-11-18
 */
@RestController
@RequestMapping("/manage/ho-enterprise-mission")
public class HoEnterpriseMissionController extends BaseController {
    @Autowired
    private IHoEnterpriseMissionService enterpriseMissionService;

    /**
     * @description: 查询企业使命列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [enterpriseMission]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.CORPORATE_MISSION,business = EOperationBusiness.CORPORATE_MISSION, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(HoEnterpriseMissionExt enterpriseMission) {
        startPage();
        QueryWrapper<HoEnterpriseMission> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(enterpriseMission.getStartDate())
                &&StringUtils.isNotNull(enterpriseMission.getEndDate())){
            queryWrapper.between("CREATION_TIME", enterpriseMission.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseMission.getEndDate()));
        }
        if(StringUtils.isNotNull(enterpriseMission.getStartEffectDate())
                &&StringUtils.isNotNull(enterpriseMission.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", enterpriseMission.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseMission.getEndEffectDate()));
        }
        if(StringUtils.isNotEmpty(enterpriseMission.getTitleName())){
            queryWrapper.like("TITLE_NAME",enterpriseMission.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoEnterpriseMission> list = enterpriseMissionService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/7/19 15:34
     * @param: [response, enterpriseMission]
     * @return: void
     **/
    @Log(module = EOperationModule.CORPORATE_MISSION,business = EOperationBusiness.CORPORATE_MISSION, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoEnterpriseMissionExt enterpriseMission) {
        QueryWrapper<HoEnterpriseMission> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(enterpriseMission.getStartDate())
                &&StringUtils.isNotNull(enterpriseMission.getEndDate())){
            queryWrapper.between("CREATION_TIME", enterpriseMission.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseMission.getEndDate()));
        }
        if(StringUtils.isNotNull(enterpriseMission.getStartEffectDate())
                &&StringUtils.isNotNull(enterpriseMission.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", enterpriseMission.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseMission.getEndEffectDate()));
        }
        if(StringUtils.isNotEmpty(enterpriseMission.getTitleName())){
            queryWrapper.like("TITLE_NAME",enterpriseMission.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoEnterpriseMissionVO> newlist=new ArrayList<>();
        List<HoEnterpriseMission> list = enterpriseMissionService.list(queryWrapper);
        list.forEach(item -> newlist.add(BeanUtil.copyProperties(item, HoEnterpriseMissionVO.class)));
        ExcelUtil<HoEnterpriseMissionVO> util = new ExcelUtil<>(HoEnterpriseMissionVO.class);
        util.exportExcel(response, newlist, "企业使命");
    }


    /**
     * @description: 新增企业使命
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [enterpriseMission]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CORPORATE_MISSION,business = EOperationBusiness.CORPORATE_MISSION, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoEnterpriseMission enterpriseMission) {
        if(StringUtils.isEmpty(enterpriseMission.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(enterpriseMission.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(enterpriseMission.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTENT_NOT_NULL));
        }
        if(StringUtils.isNull(enterpriseMission.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
//        if(StringUtils.isNull(enterpriseMission.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(enterpriseMission.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<HoEnterpriseMission> queryWrapper = new QueryWrapper();
        queryWrapper.eq("SORT",enterpriseMission.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",enterpriseMission.getLanguageType());
        HoEnterpriseMission check =enterpriseMissionService.getOne(queryWrapper);
        if(StringUtils.isNotNull(check)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_REPEAT));
        }

        enterpriseMission.setPkCreator(SecurityUtils.getUserId());
        enterpriseMission.setCreationTime(new Date());
        return toAjax(enterpriseMissionService.save(enterpriseMission));
    }


    /**
     * @description: 修改企业使命
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [enterpriseMission]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CORPORATE_MISSION,business = EOperationBusiness.CORPORATE_MISSION, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoEnterpriseMission enterpriseMission) {
        if(StringUtils.isEmpty(enterpriseMission.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(enterpriseMission.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(enterpriseMission.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTENT_NOT_NULL));
        }
        if(StringUtils.isNull(enterpriseMission.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
//        if(StringUtils.isNull(enterpriseMission.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(enterpriseMission.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<HoEnterpriseMission> queryWrapper = new QueryWrapper();
        queryWrapper.ne("PK_ID",enterpriseMission.getPkId());
        queryWrapper.eq("SORT",enterpriseMission.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",enterpriseMission.getLanguageType());
        HoEnterpriseMission check =enterpriseMissionService.getOne(queryWrapper);
        if(StringUtils.isNotNull(check)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_REPEAT));
        }
        enterpriseMission.setPkModified(SecurityUtils.getUserId());
        enterpriseMission.setModifiedTime(new Date());
        return toAjax(enterpriseMissionService.updateById(enterpriseMission));
    }


    /**
     * @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) {
        HoEnterpriseMission enterpriseMission = enterpriseMissionService.getById(pkId);
        return AjaxResult.success(enterpriseMission);
    }


    /**
     * @description: 删除企业使命
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CORPORATE_MISSION,business = EOperationBusiness.CORPORATE_MISSION, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoEnterpriseMission> 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(enterpriseMissionService.update(updateWrapper));
    }
}
