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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.DateUtils;
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.BdRetailRange;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.param.BdRetailRangeParam;
import com.hzs.system.config.service.IBdRetailRangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 新零售极差配置 前端控制器
 *
 * @author zhangjing
 * @since 2022-09-09
 */
@Slf4j
@RestController
@RequestMapping("/manage/retail")
public class RetailRangeController extends BaseController {

    @Autowired
    private IBdRetailRangeService retailRangeService;

    /**
     * @description: 新增条形码配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [barCode]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_RETAIL_RANGE, business = EOperationBusiness.BD_RETAIL_RANGE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdRetailRangeParam retailRangeParam) {
        if (retailRangeParam.getRangeType() == null || retailRangeParam.getPkLevel() == null ||
                retailRangeParam.getMoneyRatio() == null || retailRangeParam.getAchieveRatio() == null ||
                retailRangeParam.getSystemType() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        if (retailRangeParam.getPkId() == null) {
            BdRetailRange bdRetailRange = BeanUtil.copyProperties(retailRangeParam, BdRetailRange.class);
            bdRetailRange.setPkCreator(SecurityUtils.getUserId());
            bdRetailRange.setPkCountry(SecurityUtils.getPkCountry());
            retailRangeService.save(bdRetailRange);
        } else {
            BdRetailRange bdRetailRange = retailRangeService.getById(retailRangeParam.getPkId());
            if (bdRetailRange == null) {
                return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
            }
            bdRetailRange.setOrderType(retailRangeParam.getOrderType());
            bdRetailRange.setRangeType(retailRangeParam.getRangeType());
            bdRetailRange.setPkLevel(retailRangeParam.getPkLevel());
            bdRetailRange.setMoneyRatio(retailRangeParam.getMoneyRatio());
            bdRetailRange.setAchieveRatio(retailRangeParam.getAchieveRatio());
            bdRetailRange.setSystemType(retailRangeParam.getSystemType());
            bdRetailRange.setModifiedTime(DateUtils.currentDateTime());
            bdRetailRange.setPkModified(SecurityUtils.getUserId());
            retailRangeService.updateById(bdRetailRange);
        }
        return AjaxResult.success();
    }

    /**
     * @description: 查询提现设置列表 理论每个国家只有一个提现配置
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [withdrawalParam]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BD_RETAIL_RANGE, business = EOperationBusiness.BD_RETAIL_RANGE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list() {
        return getDataTable(retailRangeService.list());
    }


    /**
     * @description: 删除提现设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_RETAIL_RANGE, business = EOperationBusiness.BD_RETAIL_RANGE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        BdRetailRange bdRetailRange = retailRangeService.getById(pkId);
        if (bdRetailRange == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        UpdateWrapper<BdRetailRange> 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", DateUtils.currentDateTime());
        retailRangeService.update(updateWrapper);
        return AjaxResult.success();
    }
}
