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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLabelTemplateService;
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.model.platform.LabelTemplateModel;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.i3plus.pojo.platform.bean.SysLabelTemplate;
import cn.estsh.i3plus.pojo.platform.bean.SysLabelTemplateParam;
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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 打印模板管理服务
 * @Reference :
 * @Author : siliter
 * @Date : 2019-03-18 13:15
 * @Modify :
 **/
@RestController
@Api(description = "打印模板管理服务")
@RequestMapping(PlatformConstWords.BASE_URL +"/sys-label-template")
public class SysLabelTemplateController extends CoreBaseController {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysLabelTemplateController.class);

    @Autowired
    private ISysLabelTemplateService sysLabelTemplateService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加打印模板",notes = "添加打印模板")
    public ResultBean insertSysLabelTemplate(SysLabelTemplate sysLabelTemplate) {
        try {
            // 登录用户
            SessionUser user = AuthUtil.getSessionUser();

            // 条件验证
            ValidatorBean.beginValid(sysLabelTemplate)
                    .notNull("templateCode", sysLabelTemplate.getTemplateCode())
                    .notNull("templateName", sysLabelTemplate.getTemplateName())
                    .notNull("paramsPack", sysLabelTemplate.getParamsPack())
                    .notNull("templateContent", sysLabelTemplate.getTemplateContent());

            //修改初始化
            ConvertBean.modelInitialize(sysLabelTemplate, user);

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

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "根据id删除打印模板", notes = "根据id删除打印模板")
    public ResultBean deleteSysLabelTemplate(@PathVariable("id") String idStr) {
        try {
            // 条件验证
            ValidatorBean.checkNotNull(idStr,"id不能为空");
            if(sysLabelTemplateService.getSysLabelTemplateById(Long.parseLong(idStr)) == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("打印模板不存在")
                        .setErrorSolution("请检查打印模板编号")
                        .build();
            }

            sysLabelTemplateService.deleteSysLabelTemplateById(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);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "修改打印模板",notes = "修改打印模板")
    public ResultBean updateSysLabelTemplate(SysLabelTemplate sysLabelTemplate){
        try {
            // 登录用户
            SessionUser user = AuthUtil.getSessionUser();

            // 条件验证
            ValidatorBean.beginValid(sysLabelTemplate)
                    .notNull("templateCode", sysLabelTemplate.getTemplateCode())
                    .notNull("templateName", sysLabelTemplate.getTemplateName())
                    .notNull("paramsPack", sysLabelTemplate.getParamsPack())
                    .notNull("templateContent", sysLabelTemplate.getTemplateContent());

            //添加初始化
            ConvertBean.modelUpdate(sysLabelTemplate, user);

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

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询全部打印模板",notes = "查询全部打印模板")
    public ResultBean findSysConfigAll(){
        try {
            List<SysLabelTemplate> sysLabelTemplateList = sysLabelTemplateService.ListSysLabelTemplate();
            return  ResultBean.success("操作成功")
                                .setResultList(sysLabelTemplateList)
                                .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation(value = "根据id查询打印模板",notes = "根据id查询打印模板")
    public ResultBean getSysLabelTemplateById(@PathVariable("id") String id){
        try {
            ValidatorBean.checkNotNull(id,"id不能为空");

            SysLabelTemplate sysLabelTemplate = sysLabelTemplateService.getSysLabelTemplateById(Long.parseLong(id));
            if (sysLabelTemplate != null) {
                return ResultBean.success("操作成功")
                        .setResultObject(sysLabelTemplate).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
            }else {
                return ResultBean.fail("数据不存在").setCode(ResourceEnumUtil.MESSAGE.EMPTY.getCode());
            }
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query")
    @ApiOperation(value = "打印模板复杂查询，分页，排序",notes = "打印模板复杂查询，分页，排序")
    public ResultBean querySysLabelTemplateByPager(SysLabelTemplate sysLabelTemplate,Pager pager) {
        try {
            ListPager sysConfigListPager = sysLabelTemplateService.querySysLabelTemplateByPager(sysLabelTemplate,pager);
            return  ResultBean.success("操作成功").setListPager(sysConfigListPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/find-template-id/{id}")
    @ApiOperation(value = "根据打印模板编号查询模板参数",notes = "根据打印模板编号查询模板参数")
    public ResultBean getSysLabelTemplateByTemplateId(@PathVariable("id") String id) {
        try {
            ValidatorBean.checkNotNull(id, "打印模板编号不能为空");

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

    @GetMapping("/get-template-info/{id}")
    @ApiOperation(value = "根据打印模板编号查询模板",notes = "根据打印模板编号查询模板")
    public ResultBean getSysLabelTemplateInfoById(@PathVariable("id") String id) {
        try {
            ValidatorBean.checkNotNull(id, "打印模板编号不能为空");

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