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.utils.StringUtils;
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.BdAgreementDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdAgreementDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private IBdAgreementDetailService agreementDetailService;


    /**
     * @description: 查询协议配置明细列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [agreementDetail]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AGREEMENT_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdAgreementDetail agreementDetail) {
        startPage();
        QueryWrapper<BdAgreementDetail> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(agreementDetail.getPkAgreement())){
            queryWrapper.eq("PK_AGREEMENT",agreementDetail.getPkAgreement());
        }
        if(StringUtils.isNotNull(agreementDetail.getSpecialArea())){
            queryWrapper.eq("SPECIAL_AREA",agreementDetail.getSpecialArea());
        }
        queryWrapper.orderByAsc("CREATION_TIME");
        List<BdAgreementDetail> list = agreementDetailService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增协议配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [agreementDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT_DETAIL, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdAgreementDetail agreementDetail) {
        agreementDetail.setPkCountry(SecurityUtils.getPkCountry());
        if(StringUtils.isNull(agreementDetail.getPkAgreement())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_AGREEMENT_NOT_NULL));
        }
        if(StringUtils.isNull(agreementDetail.getSpecialArea())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SPECIAL_AREA_NOT_NULL));
        }
        agreementDetail.setPkCountry(SecurityUtils.getPkCountry());
        agreementDetail.setPkCreator(SecurityUtils.getUserId());
        agreementDetail.setCreationTime(new Date());
        return toAjax(agreementDetailService.save(agreementDetail));
    }


    /**
     * @description: 修改协议配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [agreementDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT_DETAIL, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdAgreementDetail agreementDetail) {
        agreementDetail.setPkCountry(SecurityUtils.getPkCountry());
        if(StringUtils.isNull(agreementDetail.getPkAgreement())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_AGREEMENT_NOT_NULL));
        }
        if(StringUtils.isNull(agreementDetail.getSpecialArea())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SPECIAL_AREA_NOT_NULL));
        }

        agreementDetail.setPkModified(SecurityUtils.getUserId());
        agreementDetail.setModifiedTime(new Date());
        return toAjax(agreementDetailService.updateById(agreementDetail));
    }


    /**
     * @description: 查询单条协议配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGREEMENT_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdAgreementDetail agreementDetail = agreementDetailService.getById(pkId);
        return AjaxResult.success(agreementDetail);
    }


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

}
