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.domain.R;
import com.hzs.common.core.enums.*;
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.BdAgent;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdAgentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
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-13
 */
@Slf4j
@RestController
@RequestMapping("/manage/agent")
public class BdAgentController extends BaseController {

    @Autowired
    private IBdAgentService agentService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * @description: 查询区代设置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [range]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AGENT, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdAgent range) {
        startPage();
        QueryWrapper<BdAgent> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(range.getAgentName())){
            queryWrapper.eq("AGENT_NAME",range.getAgentName());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdAgent> list = agentService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增区代设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [range]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGENT, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdAgent range) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        if(StringUtils.isEmpty(range.getAgentName())){
            return AjaxResult.error(ConfigMsgConstants.AGENT_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(range.getAgentValue())){
            return AjaxResult.error(ConfigMsgConstants.AGENT_VALUE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getStartValue())){
            return AjaxResult.error(ConfigMsgConstants.START_VALUE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getPkGrade())){
            return AjaxResult.error(ConfigMsgConstants.PK_GRADE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getPkAwards())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(range.getRegisterAuthority())){
            return AjaxResult.error(ConfigMsgConstants.REGISTER_AUTHORITY_NOT_NULL);
        }

        //字段翻译保存到数据库
        R<Integer> pkTransaction= iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.GRADE,
                range.getAgentName(), EYesNo.YES, EYesNo.NO);
        range.setPkTransaction(pkTransaction.getData());
        range.setCensusValue(EStatisticalValue.AMOUNT.getValue());
        range.setPkCountry(SecurityUtils.getPkCountry());
        range.setPkCreator(SecurityUtils.getUserId());
        range.setCreationTime(new Date());
        return toAjax(agentService.save(range));
    }


    /**
     * @description: 修改区代设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [range]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGENT, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdAgent range) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        if(StringUtils.isEmpty(range.getAgentName())){
            return AjaxResult.error(ConfigMsgConstants.AGENT_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(range.getAgentValue())){
            return AjaxResult.error(ConfigMsgConstants.AGENT_VALUE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getStartValue())){
            return AjaxResult.error(ConfigMsgConstants.START_VALUE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getPkGrade())){
            return AjaxResult.error(ConfigMsgConstants.PK_GRADE_NOT_NULL);
        }
        if(StringUtils.isNull(range.getPkAwards())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(range.getRegisterAuthority())){
            return AjaxResult.error(ConfigMsgConstants.REGISTER_AUTHORITY_NOT_NULL);
        }
        //字段翻译保存到数据库
        R<Integer> pkTransaction= iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.GRADE,
                range.getAgentName(), EYesNo.YES, EYesNo.NO);
        range.setPkTransaction(pkTransaction.getData());
        range.setPkModified(SecurityUtils.getUserId());
        range.setModifiedTime(new Date());
        return toAjax(agentService.updateById(range));
    }


    /**
     * @description: 查询单条区代设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AGENT, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdAgent range = agentService.getById(pkId);
        return AjaxResult.success(range);
    }


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