package com.slodon.b2b2c.model.system;

import com.slodon.b2b2c.core.constant.TplPcConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.dao.read.system.TplPcMallDataReadMapper;
import com.slodon.b2b2c.dao.read.system.TplPcReadMapper;
import com.slodon.b2b2c.dao.write.system.TplPcMallDataWriteMapper;
import com.slodon.b2b2c.system.example.TplPcMallDataExample;
import com.slodon.b2b2c.system.pojo.TplPc;
import com.slodon.b2b2c.system.pojo.TplPcMallData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class TplPcMallDataModel {

    @Resource
    private TplPcMallDataReadMapper tplPcMallDataReadMapper;
    @Resource
    private TplPcMallDataWriteMapper tplPcMallDataWriteMapper;
    @Resource
    private TplPcReadMapper tplPcReadMapper;

    /**
     * 新增PC商城装修模板实例化数据表
     *
     * @param tplPcMallData
     * @return
     */
    public Integer saveTplPcMallData(TplPcMallData tplPcMallData) {
        //查重
        TplPcMallDataExample example = new TplPcMallDataExample();
        example.setName(tplPcMallData.getName());
        example.setStoreId(tplPcMallData.getStoreId());
        example.setWebSite(tplPcMallData.getWebSite());
        int count = tplPcMallDataReadMapper.countByExample(example);
        AssertUtil.isTrue(count > 0, "模板名称重复，请重新输入");

        TplPc tplPc = tplPcReadMapper.getByPrimaryKey(tplPcMallData.getTplPcId());
        AssertUtil.notNull(tplPc, "获取装修模板信息为空");
        tplPcMallData.setTplPcName(tplPc.getName());
        tplPcMallData.setTplPcType(tplPc.getType());
        tplPcMallData.setTplPcTypeName(tplPc.getTypeName());
        AssertUtil.isTrue(tplPcMallData.getName().length() > 150, "复制后名称过长，请修改");
        count = tplPcMallDataWriteMapper.insert(tplPcMallData);
        if (count == 0) {
            throw new MallException("添加PC商城装修模板实例化数据表失败，请重试");
        }
        return tplPcMallData.getDataId();
    }

    /**
     * 根据dataId删除PC商城装修模板实例化数据表
     *
     * @param dataId  数据id
     * @param storeId 店铺id
     */
    public void deleteTplPcMallData(Integer dataId, Long storeId) {
        if (StringUtils.isEmpty(dataId)) {
            throw new MallException("请选择要删除的数据");
        }
        TplPcMallData tplPcMallData = tplPcMallDataReadMapper.getByPrimaryKey(dataId);
        AssertUtil.notNull(tplPcMallData, "无此模板实例化数据,无法删除");
        AssertUtil.isTrue(!tplPcMallData.getStoreId().equals(storeId), "非法操作");
        AssertUtil.isTrue(TplPcConst.IS_ENABLE_YES == tplPcMallData.getIsEnable(), "只有未启用的模板才可以删除");

        int count = tplPcMallDataWriteMapper.deleteByPrimaryKey(dataId);
        if (count == 0) {
            log.error("根据dataId：" + dataId + "删除PC商城装修模板实例化数据表失败");
            throw new MallException("删除PC商城装修模板实例化数据表失败,请重试");
        }
    }

    /**
     * 根据dataId更新PC商城装修模板实例化数据表
     *
     * @param tplPcMallData
     * @return
     */
    public Integer updateTplPcMallData(TplPcMallData tplPcMallData) {
        if (StringUtils.isEmpty(tplPcMallData.getDataId())) {
            throw new MallException("请选择要修改的数据");
        }
        if (!StringUtils.isEmpty(tplPcMallData.getName())){
            TplPcMallDataExample example = new TplPcMallDataExample();
            example.setName(tplPcMallData.getName());
            example.setStoreId(tplPcMallData.getStoreId());
            example.setWebSite(tplPcMallData.getWebSite());
            example.setDataIdNotEquals(tplPcMallData.getDataId());
            AssertUtil.isTrue(tplPcMallDataReadMapper.countByExample(example) > 0, "模板名称重复，请重新输入");
        }

        int count = tplPcMallDataWriteMapper.updateByPrimaryKeySelective(tplPcMallData);
        if (count == 0) {
            log.error("根据dataId：" + tplPcMallData.getDataId() + "更新PC商城装修模板实例化数据表失败");
            throw new MallException("更新PC商城装修模板实例化数据表失败,请重试");
        }
        return count;
    }

    /**
     * 根据dataId获取PC商城装修模板实例化数据表详情
     *
     * @param dataId dataId
     * @return
     */
    public TplPcMallData getTplPcMallDataByDataId(Integer dataId) {
        return tplPcMallDataReadMapper.getByPrimaryKey(dataId);
    }

    /**
     * 根据条件获取PC商城装修模板实例化数据表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<TplPcMallData> getTplPcMallDataList(TplPcMallDataExample example, PagerInfo pager) {
        List<TplPcMallData> tplPcMallDataList;
        if (pager != null) {
            pager.setRowsCount(tplPcMallDataReadMapper.countByExample(example));
            tplPcMallDataList = tplPcMallDataReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            tplPcMallDataList = tplPcMallDataReadMapper.listByExample(example);
        }
        return tplPcMallDataList;
    }
}
