package cn.estsh.i3plus.core.apiservice.controller.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysBarcodeRuleService;
import cn.estsh.i3plus.core.apiservice.util.BarcodeParseUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.i3plus.pojo.platform.bean.SysBarcodeRule;
import cn.estsh.i3plus.pojo.platform.bean.SysConfig;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description :条码规则管理服务
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2019-03-12 16:00
 * @Modify:
 **/
@RestController
@Api(description = "条码规则管理服务")
@RequestMapping(PlatformConstWords.BASE_URL +"/sys-barcode-rule")
public class SysBarcodeRuleController extends CoreBaseController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysConfigController.class);

    @Autowired
    private ISysBarcodeRuleService sysBarcodeRuleService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加条码规则",notes = "添加条码规则")
    public ResultBean insertSysBarcodeRule(SysBarcodeRule sysBarcodeRule) {
        try {
            // 条件验证
            ValidatorBean.beginValid(sysBarcodeRule)
                    .notNull("name",sysBarcodeRule.getName())
                    .notNull("barcodeRuleCode",sysBarcodeRule.getBarcodeRuleCode())
                    .notNull("barcodeRule",sysBarcodeRule.getBarcodeRule());

            sysBarcodeRuleService.insertSysBarcodeRule(sysBarcodeRule);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改条码规则",notes = "修改条码规则")
    public ResultBean updateSysBarcodeRule(SysBarcodeRule sysBarcodeRule) {
        try {
            // 条件验证
            ValidatorBean.beginValid(sysBarcodeRule)
                    .notNull("name",sysBarcodeRule.getName())
                    .notNull("barcodeRuleCode",sysBarcodeRule.getBarcodeRuleCode())
                    .notNull("barcodeRule",sysBarcodeRule.getBarcodeRule());

            sysBarcodeRuleService.updateSysBarcodeRule(sysBarcodeRule);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除条码规则",notes = "删除条码规则")
    public ResultBean deleteSysBarcodeRuleById(@PathVariable("id") String idStr) {
        try {
            // 条件验证
            ValidatorBean.checkNotNull(idStr,"id不能为空");
            SysBarcodeRule sysBarcodeRule = sysBarcodeRuleService.getSysBarcodeRuleById(Long.parseLong(idStr));
            if(sysBarcodeRule == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("条码规则不存在")
                        .setErrorSolution("请重新输入条码id")
                        .build();
            }

            sysBarcodeRuleService.deleteSysBarcodeRuleById(Long.parseLong(idStr));
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query")
    @ApiOperation(value = "条码规则分页复杂查询",notes = "条码规则分页复杂查询")
    public ResultBean querySysBarcodeRuleByPager(SysBarcodeRule sysBarcodeRule, Pager pager) {
        try {
            ListPager listPager = sysBarcodeRuleService.querySysBarcodeRuleByPager(sysBarcodeRule,pager);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setListPager(listPager);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/parser")
    @ApiOperation(value = "解析条码规则",notes = "解析条码规则")
    public ResultBean querySysBarcodeRuleByPager(String ruleCode, String barcode) {
        try {
            // 条件验证
            ValidatorBean.checkNotNull(ruleCode,"规则代码不能为空");
            SysBarcodeRule sysBarcodeRule = sysBarcodeRuleService.getSysBarcodeRuleByCode(ruleCode);
            if(sysBarcodeRule == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("条码规则不存在")
                        .setErrorSolution("请重新输入条码id")
                        .build();
            }

            // 解析条码规则
            Map resultMap = BarcodeParseUtil.parse(sysBarcodeRule,barcode);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultMap(resultMap);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
