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.HoEnterpriseNews;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.home.service.IHoEnterpriseNewsService;
import com.hzs.system.home.vo.HoEnterpriseNewsVO;
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-news")
public class HoEnterpriseNewsController extends BaseController {
    @Autowired
    private IHoEnterpriseNewsService enterpriseNewsService;

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

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/7/19 15:34
     * @param: [response, enterpriseMission]
     * @return: void
     **/
    @Log(module = EOperationModule.COMPANY_NEWS,business = EOperationBusiness.COMPANY_NEWS, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoEnterpriseMissionExt enterpriseNews) {
        QueryWrapper<HoEnterpriseNews> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(enterpriseNews.getStartDate())
                &&StringUtils.isNotNull(enterpriseNews.getEndDate())){
            queryWrapper.between("CREATION_TIME", enterpriseNews.getStartDate(), enterpriseNews.getEndDate());
        }
        if(StringUtils.isNotNull(enterpriseNews.getStartEffectDate())
                &&StringUtils.isNotNull(enterpriseNews.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", enterpriseNews.getStartEffectDate(),
                    enterpriseNews.getEndEffectDate());
        }
        if(StringUtils.isNotEmpty(enterpriseNews.getTitleName())){
            queryWrapper.like("TITLE_NAME",enterpriseNews.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoEnterpriseNewsVO> newlist =new ArrayList<>();
        List<HoEnterpriseNews> list = enterpriseNewsService.list(queryWrapper);
        list.forEach(item -> newlist.add(BeanUtil.copyProperties(item, HoEnterpriseNewsVO.class)));
        ExcelUtil<HoEnterpriseNewsVO> util = new ExcelUtil<>(HoEnterpriseNewsVO.class);
        util.exportExcel(response, newlist, "公司新闻");
    }

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

        enterpriseNews.setPkCreator(SecurityUtils.getUserId());
        enterpriseNews.setCreationTime(new Date());
        return toAjax(enterpriseNewsService.save(enterpriseNews));
    }


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


    /**
     * @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) {
        HoEnterpriseNews enterpriseNews = enterpriseNewsService.getById(pkId);
        return AjaxResult.success(enterpriseNews);
    }


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