package com.hzs.scm.warn.manage.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
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.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.scm.warn.ext.IcWarnConfigExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.scm.warn.param.WarnConfigParam;
import com.hzs.scm.warn.service.IIcWarnConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品预警配置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/manage/warn")
public class IcWarnConfigController extends BaseController {

    @Autowired
    private IIcWarnConfigService warnConfigService;

    /**
     * 查询列表
     *
     * @return warnConfigParam 入参
     */
    @PostMapping("/list")
    @Log(module = EOperationModule.SCM_WARN, business = EOperationBusiness.SCM_WARN, method = EOperationMethod.SELECT)
    public TableDataInfo listPoOrder(@RequestBody WarnConfigParam warnConfigParam) {
        startPage();
        warnConfigParam.setPkCountry(SecurityUtils.getPkCountry());
        return getDataTable(warnConfigService.queryWarnConfigByCondition(warnConfigParam));
    }

    /**
     * 新增预警配置
     *
     * @param warnConfigParam 入参
     */
    @PostMapping("/insert")
    @Log(module = EOperationModule.SCM_WARN, business = EOperationBusiness.SCM_WARN, method = EOperationMethod.INSERT)
    public AjaxResult insertWarnConfig(@RequestBody WarnConfigParam warnConfigParam) {
        // 验证供应商
        String msg = validateWarnConfigParam(warnConfigParam);
        if (ObjectUtil.isNotEmpty(msg)) {
            return AjaxResult.error(TransactionUtils.getContent(msg));
        }
        warnConfigService.saveWarnConfigByParam(warnConfigParam);
        return AjaxResult.success();
    }

    /**
     * 修改采购入库
     *
     * @param warnConfigParam 入参
     */
    @PostMapping("/update")
    @Log(module = EOperationModule.SCM_WARN, business = EOperationBusiness.SCM_WARN, method = EOperationMethod.UPDATE)
    public AjaxResult updateWarnConfig(@RequestBody WarnConfigParam warnConfigParam) {
        if (warnConfigParam.getPkId() == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        // 验证供应商
        String msg = validateWarnConfigParam(warnConfigParam);
        if (ObjectUtil.isNotEmpty(msg)) {
            return AjaxResult.error(TransactionUtils.getContent(msg));
        }
        warnConfigService.saveWarnConfigByParam(warnConfigParam);
        return AjaxResult.success();
    }

    private String validateWarnConfigParam(WarnConfigParam warnConfigParam){
        warnConfigParam.setPkCountry(SecurityUtils.getPkCountry());
        warnConfigParam.setPkCreator(SecurityUtils.getUserId());
        if(warnConfigParam.getPkProductBas() == null || warnConfigParam.getWarnPeriod() == null ||
                warnConfigParam.getWarnQuantity() == null || CollectionUtil.isEmpty(warnConfigParam.getScmItemSpecsList())){
            return MemberMsgConstants.REQUIRED_NOT_EMPTY;
        }
        // 验证是否存在该产品的预警
        WarnConfigParam queryParam = new WarnConfigParam();
        queryParam.setPkProductBas(warnConfigParam.getPkProductBas());
        queryParam.setPkId(warnConfigParam.getPkId());
        queryParam.setScmItemSpecsList(warnConfigParam.getScmItemSpecsList());
        List<IcWarnConfigExt> icWarnConfigExtList = warnConfigService.queryWarnConfigByCondition(queryParam);
        if(CollectionUtil.isNotEmpty(icWarnConfigExtList)){
            return ScmMsgConstants.PRODUCT_HAS_EXIST;
        }
        return null;
    }

    /**
     * 删除采购入库
     *
     * @param pkId 主键
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.SCM_WARN, business = EOperationBusiness.SCM_WARN, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Integer pkId) {
        // 验证是否存在订单，订单状态是否是保存状态
        if (pkId == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        }
        warnConfigService.deleteWarnConfigById(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
