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


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.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EYesNo;
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.config.BdAgreement;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.config.service.IBdAgreementService;
import com.hzs.system.config.vo.BdAgreementVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 协议配置表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/manage/agreement")
@Slf4j
public class BdAgreementController extends BaseController {

    @Autowired
    private IBdAgreementService agreementService;

    /**
     * @description: 查询协议配置列表
     * @author: zhang jing
     * @date: 2022/8/29 9:56
     * @param: [agreement]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdAgreementVO agreement) {
        startPage();
        QueryWrapper<BdAgreement> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(agreement.getTitle())) {
            queryWrapper.eq("TITLE", agreement.getTitle());
        }

        // 创建时间筛选
        if (agreement.getStartDate() != null && agreement.getEndDate() != null) {
            queryWrapper.between("CREATION_TIME", agreement.getStartDate(), agreement.getEndDate());
        }
        // 生效筛选
        if (agreement.getEffectiveEndDate() != null && agreement.getEffectiveStartDate() != null) {
            queryWrapper.between("EFFECTIVE_DATE", agreement.getEffectiveStartDate(), agreement.getEffectiveEndDate() );
        }

        if (StringUtils.isNotNull(agreement.getIsForcedRead())) {
            queryWrapper.eq("IS_FORCED_READ", agreement.getIsForcedRead());
        }
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("EFFECTIVE_DATE");
        List<BdAgreement> list = agreementService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增协议配置
     * @author: zhang jing
     * @date: 2022/8/29 9:56
     * @param: [agreement]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdAgreement agreement) {
        agreement.setPkCountry(SecurityUtils.getPkCountry());
        if(StringUtils.isNull(agreement.getTitle())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PROTOCOL_TITLE_NOT_NULL));
        }
        if(StringUtils.isNull(agreement.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PROTOCOL_CONTENT_NOT_NULL));
        }

//        if(StringUtils.isNull(agreement.getIsForcedRead())){
//            return AjaxResult.error(ConfigMsgConstants.IS_FORCED_READ_NOT_NULL);
//        }else{
//            if(StringUtils.isNull(agreement.getForcedReadTime())){
//                return AjaxResult.error(ConfigMsgConstants.FORCED_READ_TIME_NOT_NULL);
//            }
//        }

        if(StringUtils.isNull(agreement.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        agreement.setPkCreator(SecurityUtils.getUserId());
        agreement.setCreationTime(new Date());
        agreement.setPkCountry(SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(agreement.getIsIndexAlert())&&agreement.getIsIndexAlert() == EYesNo.YES.getIntValue()){
            // 是的情况下
            UpdateWrapper<BdAgreement> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
            updateWrapper.set("IS_INDEX_ALERT",EYesNo.YES.getIntValue());
            agreementService.update(updateWrapper);
        }
        if (agreementService.save(agreement)) {
            return AjaxResult.success();
        }
        return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.FAILED_TO_SAVE));
    }

    /**
     * @description: 查询协议配置
     * @author: zhang jing
     * @date: 2022/8/29 9:56
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.SELECT)
    @GetMapping("/{pkId}")
    public AjaxResult modify(@PathVariable Long pkId) {
        BdAgreement agreement = agreementService.getById(pkId);
        return AjaxResult.success(agreement);
    }

    /**
     * @description: 协议配置导出
     * @author: zhang jing
     * @date: 2022/8/29 9:57
     * @param: [response, agreement]
     * @return: void
     **/
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, BdAgreement agreement) {
        QueryWrapper<BdAgreement> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(agreement.getTitle())) {
            queryWrapper.eq("TITLE", agreement.getTitle());
        }
        if (StringUtils.isNotNull(agreement.getEffectiveDate())) {
            queryWrapper.eq("EFFECTIVE_DATE", agreement.getEffectiveDate());
        }
        List<BdAgreement> list = agreementService.list(queryWrapper);
        ExcelUtil<BdAgreement> util = new ExcelUtil<>(BdAgreement.class);
        util.exportExcel(response, list, "协议配置导出");
    }

    /**
     * @description: 删除协议配置
     * @author: zhang jing
     * @date: 2022/8/29 9:57
     * @param: [agreement]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdAgreement> 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(agreementService.update(updateWrapper));
    }

    /**
     * @description: 修改协议配置信息
     * @author: zhang jing
     * @date: 2022/8/29 9:57
     * @param: [agreement]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdAgreement agreement){
        agreement.setPkCountry(SecurityUtils.getPkCountry());
        if(StringUtils.isNull(agreement.getTitle())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PROTOCOL_TITLE_NOT_NULL));
        }
        if(StringUtils.isNull(agreement.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PROTOCOL_CONTENT_NOT_NULL));
        }
//        if(StringUtils.isNull(agreement.getIsForcedRead())){
//            return AjaxResult.error(ConfigMsgConstants.IS_FORCED_READ_NOT_NULL);
//        }else{
//            if(StringUtils.isNull(agreement.getForcedReadTime())){
//                return AjaxResult.error(ConfigMsgConstants.FORCED_READ_TIME_NOT_NULL);
//            }
//        }
        if(StringUtils.isNull(agreement.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        agreement.setPkModified(SecurityUtils.getUserId());
        agreement.setModifiedTime(new Date());
        agreement.setPkCountry(SecurityUtils.getPkCountry());
        if (agreement.getIsIndexAlert() == EYesNo.YES.getIntValue()){
            // 是的情况下
            UpdateWrapper<BdAgreement> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
            updateWrapper.set("IS_INDEX_ALERT",EYesNo.YES.getIntValue());
            agreementService.update(updateWrapper);
        }
        return toAjax(agreementService.updateById(agreement));
    }

    /**
     * 协议查询全部
     * @param agreement
     * @return
     */
    @Log(module = EOperationModule.AGREEMENT, method = EOperationMethod.SELECT)
    @GetMapping("/find-all")
    public AjaxResult findAll(BdAgreement agreement) {

        QueryWrapper<BdAgreement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        List<BdAgreement> list = agreementService.list(queryWrapper);
        return AjaxResult.success(list);
    }
}
