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

import cn.estsh.i3plus.core.api.iservice.busi.ISysLabelTemplateService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.model.platform.LabelTemplateModel;
import cn.estsh.i3plus.pojo.platform.bean.SysLabelTemplate;
import cn.estsh.i3plus.pojo.platform.bean.SysLabelTemplateParam;
import cn.estsh.i3plus.pojo.platform.repository.SysLabelTemplateParamRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysLabelTemplateRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
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.stereotype.Service;

import java.util.List;

/**
 * @Description : 打印模板服务接口实现
 * @Reference :
 * @Author : siliter
 * @Date : 2019-03-18 9:25
 * @Modify :
 **/
@Service
public class SysLabelTemplateService implements ISysLabelTemplateService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysLabelTemplateService.class);

    @Autowired
    private SysLabelTemplateRepository sysLabelTemplateRDao;

    @Autowired
    private SysLabelTemplateParamRepository sysLabelTemplateParamRDao;

    @Override
    @ApiOperation(value = "添加打印模板",notes = "添加打印模板")
    public void insertSysLabelTemplate(SysLabelTemplate sysLabelTemplate) {
        // 根据模板代码进行唯一性校验
        int count = sysLabelTemplateRDao.findByHqlWhereCount(CoreHqlPack.packHqlSysLabelTemplate(sysLabelTemplate));
        if (count > 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("打印模板已存在")
                    .setErrorSolution("请检查打印模板")
                    .build();
        }

        SysLabelTemplate insertResult = sysLabelTemplateRDao.insert(sysLabelTemplate);
        // 新增打印模板成功
        if (insertResult != null) {
            // 判断模板参数是否存在，存在就进行数据操作
            String paramPack = sysLabelTemplate.getParamsPack();
            if (StringUtils.isNotBlank(paramPack)) {
                // 新增打印模板参数
                SysLabelTemplateParam templateParam;
                String[] paramArr = paramPack.trim().split(",");
                for (String paramItem : paramArr) {
                    templateParam = new SysLabelTemplateParam();
                    templateParam.setTemplateCode(sysLabelTemplate.getTemplateCode());
                    templateParam.setTemplateId(insertResult.getId());
                    templateParam.setTemplateParam(paramItem);
                    templateParam.setTemplateParamText(paramItem);
                    ConvertBean.serviceModelInitialize(templateParam, AuthUtil.getSessionUser().getUserName());
                    sysLabelTemplateParamRDao.insert(templateParam);
                }
            }
        }
    }

    @Override
    @ApiOperation(value = "删除打印模板",notes = "删除打印模板")
    public void deleteSysLabelTemplateById(Long id) {
        sysLabelTemplateRDao.deleteById(id);
        sysLabelTemplateParamRDao.deleteByProperty("templateId", id);
    }

    @Override
    @ApiOperation(value = "修改打印模板",notes = "修改打印模板")
    public void updateSysLabelTemplate(SysLabelTemplate sysLabelTemplate) {
        SysLabelTemplate updateBefore = sysLabelTemplateRDao.getById(sysLabelTemplate.getId());
        if (updateBefore == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.WMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("打印模板不存在")
                    .build();
        }

        //暂时根据id和模板代码作为唯一性校验
        StringBuffer where = new StringBuffer();
        HqlPack.getNumNOEqualPack(sysLabelTemplate.getId(), "id", where);
        HqlPack.getStringEqualPack(sysLabelTemplate.getTemplateCode(), "templateCode", where);
        int count = sysLabelTemplateRDao.findByHqlWhereCount(where.toString());
        if (count > 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("打印模板已存在")
                    .setErrorSolution("请检查打印模板")
                    .build();
        }

        // 更新打印模板
        sysLabelTemplateRDao.update(sysLabelTemplate);

        int effect = sysLabelTemplateParamRDao.deleteByProperty("templateId", sysLabelTemplate.getId());

        // 判断模板参数是否存在，存在就进行数据操作
        if (StringUtils.isNotBlank(sysLabelTemplate.getParamsPack()) && effect > 1) {
            // 更新打印模板参数
            SysLabelTemplateParam templateParam;
            String[] paramArr = sysLabelTemplate.getParamsPack().trim().split(",");
            for (String paramItem : paramArr) {
                templateParam = new SysLabelTemplateParam();
                templateParam.setTemplateCode(sysLabelTemplate.getTemplateCode());
                templateParam.setTemplateParam(paramItem);
                templateParam.setTemplateId(sysLabelTemplate.getId());
                templateParam.setTemplateParamText(paramItem);
                ConvertBean.serviceModelInitialize(templateParam, AuthUtil.getSessionUser().getUserName());
                sysLabelTemplateParamRDao.insert(templateParam);
            }
        }
    }

    @Override
    @ApiOperation(value = "查询打印模板",notes = "查询打印模板")
    public List<SysLabelTemplate> ListSysLabelTemplate() {
        return sysLabelTemplateRDao.list();
    }

    @Override
    @ApiOperation(value = "根据编号打印模板",notes = "根据编号打印模板")
    public SysLabelTemplate getSysLabelTemplateById(Long id) {
        return sysLabelTemplateRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "复杂分页查询打印模板",notes = "复杂分页查询打印模板")
    public ListPager querySysLabelTemplateByPager(SysLabelTemplate sysLabelTemplate, Pager pager) {
        if(sysLabelTemplate == null) {
            pager = PagerHelper.getPager(pager, sysLabelTemplateRDao.listCount());
            return new ListPager(sysLabelTemplateRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysLabelTemplate(sysLabelTemplate);
            pager = PagerHelper.getPager(pager, sysLabelTemplateRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(sysLabelTemplateRDao.findByHqlWherePage(hqlPack + sysLabelTemplate.orderBy(), pager), pager);
        }
    }

    @Override
    @ApiOperation(value = "根据模板编号查询模板参数",notes = "根据模板编号查询模板参数")
    public List<SysLabelTemplateParam> listSysLabelTemplateParam(String labelTemplateId) {
        SysLabelTemplateParam templateParam = new SysLabelTemplateParam();
        templateParam.setTemplateId(Long.parseLong(labelTemplateId));
        return sysLabelTemplateParamRDao.findByHqlWhere(CoreHqlPack.packHqlSysLabelTemplateParams(templateParam));
    }

    @Override
    public LabelTemplateModel getLabelTemplateInfoById(String labelTemplateId) {
        LabelTemplateModel templateModel = new LabelTemplateModel();
        templateModel.setLabelTemplate(sysLabelTemplateRDao.getById(Long.parseLong(labelTemplateId)));
        templateModel.setTemplateParamList(listSysLabelTemplateParam(labelTemplateId));
        return templateModel;
    }
}
