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.annotation.RepeatSubmit;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
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.BdAreaCurrencyDetail;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyDetailExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdAreaCurrencyDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消费详情配置表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-08-26
 */
@Slf4j
@RestController
@RequestMapping("/manage/area-currency-detail")
public class BdAreaCurrencyDetailController extends BaseController {

    @Autowired
    private IBdAreaCurrencyDetailService areaCurrencyDetailService;

    /**
     * @description: 查询消费详情配置列表
     * @author: zhang jing
     * @date: 2022/8/29 9:53
     * @param: [areaCurrencyDetail]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AREA_CURRENCY_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdAreaCurrencyDetailExt areaCurrencyDetailExt) {
        areaCurrencyDetailExt.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        List<BdAreaCurrencyDetailExt> list = areaCurrencyDetailService.getAreaCurrencyDetailList(areaCurrencyDetailExt);
        return getDataTable(list);
    }

    /**
     * @description: 新增消费详情配置
     * @author: zhang jing
     * @date: 2022/8/29 9:52
     * @param: [areaCurrencyDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AREA_CURRENCY_DETAIL, method = EOperationMethod.INSERT)
    @RepeatSubmit
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdAreaCurrencyDetail areaCurrencyDetail) {
        if (StringUtils.isNull(areaCurrencyDetail.getPkAccount())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ACCOUNT_NOT_NULL));
        }
        areaCurrencyDetail.setPkCountry(SecurityUtils.getPkCountry());
        areaCurrencyDetail.setPkCreator(SecurityUtils.getUserId());
        areaCurrencyDetail.setCreationTime(new Date());
        QueryWrapper<BdAreaCurrencyDetail> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_AREA_CURRENCY", areaCurrencyDetail.getPkAreaCurrency());
        queryWrapper.eq("DEDUCT_SORT", areaCurrencyDetail.getDeductSort());
        BdAreaCurrencyDetail checkACD = areaCurrencyDetailService.getOne(queryWrapper);
        if (StringUtils.isNotNull(checkACD)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_SORT_NOT_REPEAT));
        }
        BigDecimal maxDR = new BigDecimal(100);
        BdAreaCurrencyDetail sumACD = areaCurrencyDetailService.getSumDeductRatio(areaCurrencyDetail);
        if (StringUtils.isNotNull(sumACD)) {
            BigDecimal sumDR = sumACD.getDeductRatio();
            if (sumDR.compareTo(maxDR) < 0) {
                //可用扣款比例
                BigDecimal usableDR = maxDR.subtract(sumDR);
                if (usableDR.compareTo(areaCurrencyDetail.getDeductRatio()) < 0) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_RATIO_NOT_MORE_THAN + usableDR));
                }
            } else {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_RATIO_FULL));
            }
        }
        return toAjax(areaCurrencyDetailService.save(areaCurrencyDetail));
    }


    /**
     * @description: 修改消费详情配置
     * @author: zhang jing
     * @date: 2022/8/29 9:49
     * @param: [areaCurrencyDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AREA_CURRENCY_DETAIL, method = EOperationMethod.UPDATE)
    @RepeatSubmit
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdAreaCurrencyDetail areaCurrencyDetail) {
        areaCurrencyDetail.setPkModified(SecurityUtils.getUserId());
        areaCurrencyDetail.setModifiedTime(new Date());
        BdAreaCurrencyDetail checkACD = areaCurrencyDetailService.checkDeductSort(areaCurrencyDetail);
        if (StringUtils.isNotNull(checkACD)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_SORT_NOT_REPEAT));
        }
        BigDecimal maxDR = new BigDecimal(100);
        BigDecimal minDR = new BigDecimal(0);
        BdAreaCurrencyDetail sumACD = areaCurrencyDetailService.getSumDeductRatio(areaCurrencyDetail);
        if (StringUtils.isNotNull(sumACD)) {
            BigDecimal sumDR = sumACD.getDeductRatio();
            BigDecimal usableDR = maxDR.subtract(sumDR);
            if (usableDR.compareTo(minDR) > 0) {
                if (usableDR.compareTo(areaCurrencyDetail.getDeductRatio()) < 0) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_RATIO_NOT_MORE_THAN + usableDR));
                }
            } else {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.DEDUCT_RATIO_FULL));
            }
        }
        return toAjax(areaCurrencyDetailService.updateById(areaCurrencyDetail));
    }


    /**
     * @description: 获取单条消费详情配置
     * @author: zhang jing
     * @date: 2022/8/29 9:52
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AREA_CURRENCY_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdAreaCurrencyDetail areaCurrencyDetail = areaCurrencyDetailService.getById(pkId);
        return AjaxResult.success(areaCurrencyDetail);
    }

    /**
     * @description: 删除消费详情配置
     * @author: zhang jing
     * @date: 2022/8/29 9:52
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AREA_CURRENCY_DETAIL, method = EOperationMethod.DELETE)
    @RepeatSubmit
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdAreaCurrencyDetail> 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(areaCurrencyDetailService.update(updateWrapper));
    }

}
