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.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.system.TplPcMallDecoReadMapper;
import com.slodon.b2b2c.dao.write.system.TplPcMallDecoWriteMapper;
import com.slodon.b2b2c.system.example.TplPcMallDecoExample;
import com.slodon.b2b2c.system.pojo.TplPcMallDeco;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

@Component
@Slf4j
public class TplPcMallDecoModel {

    @Resource
    private TplPcMallDecoReadMapper tplPcMallDecoReadMapper;
    @Resource
    private TplPcMallDecoWriteMapper tplPcMallDecoWriteMapper;

    /**
     * 新增PC商城装修页组装信息表（基于实例化数据表组合）
     *
     * @param tplPcMallDeco
     * JIANGSU ZHONGHUI STAGE TECHNOLOGY CO.,LTD.
     * @return
     */
    public Integer saveTplPcMallDeco(TplPcMallDeco tplPcMallDeco,Boolean isCopy) {
        //名称查重
        TplPcMallDecoExample example = new TplPcMallDecoExample();
        example.setDecoName(tplPcMallDeco.getDecoName());
        example.setDecoType(tplPcMallDeco.getDecoType());
        example.setWebSite(tplPcMallDeco.getWebSite());
        example.setStoreId(tplPcMallDeco.getStoreId());
        List<TplPcMallDeco> tplPcMallDecos = tplPcMallDecoReadMapper.listByExample(example);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(tplPcMallDecos), "名称重复，请重试");
        if (isCopy) AssertUtil.isTrue(tplPcMallDeco.getDecoName().length() > 150, "复制后名称过长，请修改");
        int count = tplPcMallDecoWriteMapper.insert(tplPcMallDeco);
        if (count == 0) {
            throw new MallException("添加PC商城装修页组装信息表（基于实例化数据表组合）失败，请重试");
        }
        return count;
    }

    /**
     * 根据decoId删除PC商城装修页组装信息表（基于实例化数据表组合）
     *
     * @param decoId  装修页id
     * @param storeId 店铺id
     */
    public void deleteTplPcMallDeco(Integer decoId, Long storeId) {
        if (StringUtils.isEmpty(decoId)) {
            throw new MallException("请选择要删除的数据");
        }
        TplPcMallDeco tplPcMallDeco = tplPcMallDecoReadMapper.getByPrimaryKey(decoId);
        AssertUtil.notNull(tplPcMallDeco, "无此模板实例化数据,无法删除");
        AssertUtil.isTrue(!tplPcMallDeco.getStoreId().equals(storeId), "非法操作");
        AssertUtil.isTrue(TplPcConst.IS_ENABLE_YES == tplPcMallDeco.getIsEnable(), "只有未启用的模板才可以删除");

        int count = tplPcMallDecoWriteMapper.deleteByPrimaryKey(decoId);
        if (count == 0) {
            log.error("根据decoId：" + decoId + "删除PC商城装修页组装信息表（基于实例化数据表组合）失败");
            throw new MallException("删除PC商城装修页组装信息表（基于实例化数据表组合）失败,请重试");
        }
    }

    /**
     * 根据decoId更新PC商城装修页组装信息表（基于实例化数据表组合）
     *
     * @param tplPcMallDeco
     * @return
     */
    public void updateTplPcMallDecoByExample(TplPcMallDeco tplPcMallDeco, TplPcMallDecoExample example) {
        tplPcMallDecoWriteMapper.updateByExampleSelective(tplPcMallDeco, example);
    }

    /**
     * 根据decoId更新PC商城装修页组装信息表（基于实例化数据表组合）
     *
     * @param tplPcMallDeco
     * @return
     */
    public Integer updateTplPcMallDeco(TplPcMallDeco tplPcMallDeco) {
        if (StringUtils.isEmpty(tplPcMallDeco.getDecoId())) {
            throw new MallException("请选择要修改的数据");
        }

        if (!StringUtil.isEmpty(tplPcMallDeco.getDecoName())) {
            //名称查重
            TplPcMallDecoExample example = new TplPcMallDecoExample();
            example.setDecoIdNotEquals(tplPcMallDeco.getDecoId());
            example.setDecoName(tplPcMallDeco.getDecoName());
            example.setDecoType(tplPcMallDeco.getDecoType());
            example.setWebSite(tplPcMallDeco.getWebSite());
            example.setStoreId(tplPcMallDeco.getStoreId());
            List<TplPcMallDeco> tplPcMallDecos = tplPcMallDecoReadMapper.listByExample(example);
            AssertUtil.isTrue(!CollectionUtils.isEmpty(tplPcMallDecos), "名称重复，请重试");
        }

        int count = tplPcMallDecoWriteMapper.updateByPrimaryKeySelective(tplPcMallDeco);
        if (count == 0) {
            log.error("根据decoId：" + tplPcMallDeco.getDecoId() + "更新PC商城装修页组装信息表（基于实例化数据表组合）失败");
            throw new MallException("更新PC商城装修页组装信息表（基于实例化数据表组合）失败,请重试");
        }
        return count;
    }

    /**
     * 根据decoId获取PC商城装修页组装信息表（基于实例化数据表组合）详情
     *
     * @param decoId decoId
     * @return
     */
    public TplPcMallDeco getTplPcMallDecoByDecoId(Integer decoId) {
        return tplPcMallDecoReadMapper.getByPrimaryKey(decoId);
    }

    /**
     * 根据条件获取PC商城装修页组装信息表（基于实例化数据表组合）列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<TplPcMallDeco> getTplPcMallDecoList(TplPcMallDecoExample example, PagerInfo pager) {
        List<TplPcMallDeco> tplPcMallDecoList;
        if (pager != null) {
            pager.setRowsCount(tplPcMallDecoReadMapper.countByExample(example));
            tplPcMallDecoList = tplPcMallDecoReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            tplPcMallDecoList = tplPcMallDecoReadMapper.listByExample(example);
        }
        return tplPcMallDecoList;
    }
}
