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


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.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EYesNo;
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.BdSubsidy;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdSubsidyService;
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 2023-02-28
 */
@RestController
@RequestMapping("/manage/bd-subsidy")
public class BdSubsidyController extends BaseController {

    @Autowired
    private IBdSubsidyService subsidyService;

    /**
     * @description: 查询补贴比例配置列表
     * @author: zhang jing
     * @date: 2023/2/27 17:59
     * @param: [bdsubsidy]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BDSUBSIDY, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdSubsidy bdsubsidy) {
        List<BdSubsidy> listVO = subsidyService.list();
        return getDataTable(listVO);
    }

    /**
     * @description: 新增补贴比例配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shop]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDSUBSIDY, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdSubsidy subsidy) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isNull(subsidy.getSubsidyProportion())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SUBSIDY_PROPORTION_NOT_NULL));
        }
        subsidy.setPkCountry(pkCountry);
        subsidy.setPkCreator(SecurityUtils.getUserId());
        subsidy.setCreationTime(new Date());
        return toAjax(subsidyService.save(subsidy));
    }

    /**
     * @description: 修改补贴比例配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shop]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDSUBSIDY, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdSubsidy subsidy) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isNull(subsidy.getPkId())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        if (subsidy.getUsageStatus().intValue() != EYesNo.YES.getIntValue()) {
            if (StringUtils.isNull(subsidy.getSubsidyProportion())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SUBSIDY_PROPORTION_NOT_NULL));
            }
        }
        subsidy.setPkCountry(pkCountry);
        subsidy.setPkModified(SecurityUtils.getUserId());
        subsidy.setModifiedTime(new Date());
        return toAjax(subsidyService.updateById(subsidy));
    }

    /**
     * @description: 查询单条补贴比例配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDSUBSIDY, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        if (StringUtils.isNull(pkId)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        BdSubsidy subsidy = subsidyService.getById(pkId);
        return AjaxResult.success(subsidy);
    }

    /**
     * @description: 删除补贴比例配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDSUBSIDY, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        if (StringUtils.isNull(pkId)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        BdSubsidy subsidy = subsidyService.getById(pkId);
        if (StringUtils.isNotNull(subsidy)) {
            if (subsidy.getUsageStatus().intValue() == EYesNo.YES.getIntValue()) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ENABLE_STATE_NOT_DELETE));
            }
            UpdateWrapper<BdSubsidy> 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());
            subsidyService.update(updateWrapper);
        }
        return toAjax(true);
    }
}
