package com.slodon.b2b2c.model.system;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.system.ZjEnterpriseTypeReadMapper;
import com.slodon.b2b2c.dao.write.system.ZjEnterpriseTypeWriteMapper;
import com.slodon.b2b2c.system.example.ZjEnterpriseTypeExample;
import com.slodon.b2b2c.system.pojo.ZjEnterpriseType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * 中金开户-企业类别model
 */
@Component
@Slf4j
public class ZjEnterpriseTypeModel {
    @Resource
    private ZjEnterpriseTypeReadMapper zjEnterpriseTypeReadMapper;

    @Resource
    private ZjEnterpriseTypeWriteMapper zjEnterpriseTypeWriteMapper;

    /**
     * 新增中金开户-企业类别
     *
     * @param zjEnterpriseType 中金开户-企业类别
     * @return
     */
    public Integer saveZjEnterpriseType(ZjEnterpriseType zjEnterpriseType) {
        int count = zjEnterpriseTypeWriteMapper.insert(zjEnterpriseType);
        if (count == 0) {
            throw new MallException("添加中金开户-企业类别失败，请重试");
        }
        return count;
    }

    /**
     * 根据typeId删除中金开户-企业类别
     *
     * @param typeId typeId
     * @return
     */
    public Integer deleteZjEnterpriseType(Integer typeId) {
        if (StringUtils.isEmpty(typeId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = zjEnterpriseTypeWriteMapper.deleteByPrimaryKey(typeId);
        if (count == 0) {
            log.error("根据typeId：" + typeId + "删除中金开户-企业类别失败");
            throw new MallException("删除中金开户-企业类别失败,请重试");
        }
        return count;
    }

    /**
     * 根据typeId更新中金开户-企业类别
     *
     * @param zjEnterpriseType 中金开户-企业类别
     * @return
     */
    public Integer updateZjEnterpriseType(ZjEnterpriseType zjEnterpriseType) {
        if (StringUtils.isEmpty(zjEnterpriseType.getTypeId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = zjEnterpriseTypeWriteMapper.updateByPrimaryKeySelective(zjEnterpriseType);
        if (count == 0) {
            log.error("根据typeId：" + zjEnterpriseType.getTypeId() + "更新中金开户-企业类别失败");
            throw new MallException("更新中金开户-企业类别失败,请重试");
        }
        return count;
    }

    /**
     * 根据typeId获取中金开户-企业类别详情
     *
     * @param typeId typeId
     * @return
     */
    public ZjEnterpriseType getZjEnterpriseTypeByTypeId(Integer typeId) {
        return zjEnterpriseTypeReadMapper.getByPrimaryKey(typeId);
    }

    /**
     * 根据条件获取中金开户-企业类别列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<ZjEnterpriseType> getZjEnterpriseTypeList(ZjEnterpriseTypeExample example, PagerInfo pager) {
        List<ZjEnterpriseType> zjEnterpriseTypeList;
        if (pager != null) {
            pager.setRowsCount(zjEnterpriseTypeReadMapper.countByExample(example));
            zjEnterpriseTypeList = zjEnterpriseTypeReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            zjEnterpriseTypeList = zjEnterpriseTypeReadMapper.listByExample(example);
        }
        return zjEnterpriseTypeList;
    }
}