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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.BdShare;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdShareService;
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;

/**
 * 分红配置 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/share")
public class BdShareController extends BaseController {

    @Autowired
    private IBdShareService iBdShareService;

    /**
     * 查询分红配置列表
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdShare share) {
        startPage();
        LambdaQueryWrapper<BdShare> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdShare::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotEmpty(share.getMonth())) {
            queryWrapper.eq(BdShare::getMonth, share.getMonth());
        }
        if (StringUtils.isNotNull(share.getIsGrant())) {
            queryWrapper.eq(BdShare::getIsGrant, share.getIsGrant());
        }
        queryWrapper.orderByDesc(BdShare::getCreationTime);
        List<BdShare> list = iBdShareService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增分红配置
     **/
    @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));
        }

        LambdaQueryWrapper<BdShare> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdShare::getPkCountry, pkCountry);
        if (StringUtils.isNotNull(share.getMonth())) {
            queryWrapper.eq(BdShare::getMonth, share.getMonth());
        }
        List<BdShare> list = iBdShareService.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(iBdShareService.save(share));
    }

    /**
     * 修改分红配置
     **/
    @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());
        return toAjax(iBdShareService.updateById(share));
    }

    /**
     * 查询单条分红配置
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdShare share = iBdShareService.getById(pkId);
        return AjaxResult.success(share);
    }

    /**
     * 删除分红配置
     **/
    @Log(module = EOperationModule.SHARE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<BdShare> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdShare::getPkId, pkId);
        updateWrapper.set(BdShare::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdShare::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdShare::getModifiedTime, new Date());
        return toAjax(iBdShareService.update(updateWrapper));
    }
}
