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.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.home.HoEnterpriseMission;
import com.hzs.common.domain.system.home.HoEnterpriseStyle;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.home.service.IHoEnterpriseStyleService;
import com.hzs.system.home.vo.HoEnterpriseMissionVO;
import com.hzs.system.home.vo.HoEnterpriseStyleVO;
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-19
 */
@RestController
@RequestMapping("/manage/ho-enterprise-style")
public class HoEnterpriseStyleController extends BaseController {
    @Autowired
    private IHoEnterpriseStyleService enterpriseStyleService;

    /**
     * @description: 查询公司风采列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [enterpriseStyle]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.COMPANY_SHOW,business = EOperationBusiness.COMPANY_SHOW, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(HoEnterpriseMissionExt enterpriseStyle) {
        startPage();
        QueryWrapper<HoEnterpriseStyle> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(enterpriseStyle.getStartDate())
                &&StringUtils.isNotNull(enterpriseStyle.getEndDate())){
            queryWrapper.between("CREATION_TIME", enterpriseStyle.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseStyle.getEndDate()));
        }
        if(StringUtils.isNotNull(enterpriseStyle.getStartEffectDate())
                &&StringUtils.isNotNull(enterpriseStyle.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", enterpriseStyle.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,enterpriseStyle.getEndEffectDate()));

        }
        if(StringUtils.isNotEmpty(enterpriseStyle.getTitleName())){
            queryWrapper.like("TITLE_NAME",enterpriseStyle.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoEnterpriseStyle> list = enterpriseStyleService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/7/19 15:34
     * @param: [response, enterpriseMission]
     * @return: void
     **/
    @Log(module = EOperationModule.COMPANY_SHOW,business = EOperationBusiness.COMPANY_SHOW, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoEnterpriseMissionExt enterpriseStyle) {
        QueryWrapper<HoEnterpriseStyle> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(enterpriseStyle.getStartDate())
                &&StringUtils.isNotNull(enterpriseStyle.getEndDate())){
            queryWrapper.between("CREATION_TIME", enterpriseStyle.getStartDate(), enterpriseStyle.getEndDate());
        }
        if(StringUtils.isNotNull(enterpriseStyle.getStartEffectDate())
                &&StringUtils.isNotNull(enterpriseStyle.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", enterpriseStyle.getStartEffectDate(),
                    enterpriseStyle.getEndEffectDate());

        }
        if(StringUtils.isNotEmpty(enterpriseStyle.getTitleName())){
            queryWrapper.like("TITLE_NAME",enterpriseStyle.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoEnterpriseStyleVO> newlist =new ArrayList<>();
        List<HoEnterpriseStyle> list = enterpriseStyleService.list(queryWrapper);
        list.forEach(item -> newlist.add(BeanUtil.copyProperties(item, HoEnterpriseStyleVO.class)));
        ExcelUtil<HoEnterpriseStyleVO> util = new ExcelUtil<>(HoEnterpriseStyleVO.class);
        util.exportExcel(response, newlist, "公司风采");
    }


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

        enterpriseStyle.setPkCreator(SecurityUtils.getUserId());
        enterpriseStyle.setCreationTime(new Date());
        return toAjax(enterpriseStyleService.save(enterpriseStyle));
    }


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


    /**
     * @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) {
        HoEnterpriseStyle enterpriseStyle = enterpriseStyleService.getById(pkId);
        return AjaxResult.success(enterpriseStyle);
    }


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