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


import cn.hutool.core.collection.CollectionUtil;
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.CacheConstants;
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.service.RedisService;
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.BdShare;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdShareService;
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-22
 */
@Slf4j
@RestController
@RequestMapping("/manage/share")
public class BdShareController extends BaseController {
    @Autowired
    private IBdShareService shareService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;
    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }


    /**
     * @description: 查询分红配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [share]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdShare share) {
        startPage();
        QueryWrapper<BdShare> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotEmpty(share.getMonth())){
            queryWrapper.eq("MONTH",share.getMonth());
        }
        if(StringUtils.isNotNull(share.getIsGrant())){
            queryWrapper.eq("IS_GRANT",share.getIsGrant());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdShare> list = shareService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增分红配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [share]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdShare share) {
         Integer pkCountry =SecurityUtils.getPkCountry();
        if(StringUtils.isEmpty(share.getMonth())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MONTH_NOT_NULL));
        }
        if(StringUtils.isNull(share.getTotalAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TOTAL_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getAddAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ADD_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getUsedAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.USED_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getBalanceAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BALANCE_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getBaseAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BASE_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getIsGrant())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GRANT_NOT_NULL));
        }

        QueryWrapper<BdShare> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        if(StringUtils.isNotNull(share.getMonth())){
            queryWrapper.eq("MONTH",share.getMonth());
        }
        List<BdShare> list = shareService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SHARE_REPEAT));
        }
        share.setPkCountry(pkCountry);
        share.setPkCreator(SecurityUtils.getUserId());
        share.setCreationTime(new Date());
        return toAjax(shareService.save(share));
    }


    /**
     * @description: 修改分红配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [share]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdShare share) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        if(StringUtils.isNull(share.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PKID_NOT_NULL));
        }
        if(StringUtils.isEmpty(share.getMonth())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MONTH_NOT_NULL));
        }
        if(StringUtils.isNull(share.getTotalAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TOTAL_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getAddAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ADD_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getUsedAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.USED_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getBalanceAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BALANCE_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getBaseAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BASE_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(share.getIsGrant())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GRANT_NOT_NULL));
        }

        share.setPkCountry(pkCountry);
        share.setPkModified(SecurityUtils.getUserId());
        share.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_SHARE + share.getPkId());
        return toAjax(shareService.updateById(share));
    }


    /**
     * @description: 查询单条分红配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdShare share = shareService.getById(pkId);
        return AjaxResult.success(share);
    }


    /**
     * @description: 删除分红配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdShare> 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());
        redisService.deleteObject(CacheConstants.BD_SHARE + pkId);
        return toAjax(shareService.update(updateWrapper));
    }
}
