package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmAreaDistrictMapper;
import com.hc.business.mapper.ZccmClassConfigMapper;
import com.hc.business.model.ZccmAreaDistrict;
import com.hc.business.model.ZccmClassConfig;
import com.hc.business.service.ZccmClassConfigService;
import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.status.CodeFlagEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 班次表(ZccmClassConfig)表服务实现类
 *
 * @author java
 * @since 2020-11-19 16:12:33
 */

@Service("classConfigService")
public class ZccmClassConfigServiceImpl implements ZccmClassConfigService {

    private final ZccmClassConfigMapper classConfigMapper;

    private final ZccmAreaDistrictMapper areaDistrictMapper;

    public ZccmClassConfigServiceImpl(ZccmClassConfigMapper classConfigMapper, ZccmAreaDistrictMapper areaDistrictMapper) {
        this.classConfigMapper = classConfigMapper;
        this.areaDistrictMapper = areaDistrictMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param classConfigId 主键
     * @return 实例对象
     */
    @Override
    public ZccmClassConfig queryById(Integer classConfigId) {
        return this.classConfigMapper.selectByPrimaryKey(classConfigId);
    }

    /**
     * 新增数据
     *
     * @param classConfig 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmClassConfig insert(ZccmClassConfig classConfig) {
        classConfig.setValid(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
        classConfig.setCreateTime(System.currentTimeMillis());
        this.classConfigMapper.insert(classConfig);
        return classConfig;
    }

    /**
     * 修改数据
     *
     * @param classConfig 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmClassConfig update(ZccmClassConfig classConfig) {
        this.classConfigMapper.updateByPrimaryKeySelective(classConfig);
        return this.queryById(classConfig.getClassConfigId());
    }

    /**
     * 通过主键删除数据
     *
     * @param classConfigId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer classConfigId) {
        return this.classConfigMapper.deleteByPrimaryKey(classConfigId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        PageHelper.startPage(page, size);
        List<ZccmClassConfig> classConfigs = this.classConfigMapper.queryAll();
        List<ZccmClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

    @Override
    public PageInfo queryByValid(Integer valid, int page, int size) {
        PageHelper.startPage(page, size);
        List<ZccmClassConfig> classConfigs = this.classConfigMapper.queryByValid(valid);
        List<ZccmClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

    @Override
    public List<ZccmClassConfig> queryByClassName(String name) {
        return this.classConfigMapper.queryByClassName(name);
    }

    @Override
    public PageInfo queryByAreaId(Integer areaId, int page, int size) {
        PageHelper.startPage(page, size);
        List<ZccmClassConfig> classConfigs = this.classConfigMapper.queryByAreaId(areaId);
        List<ZccmClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

    @Override
    public PageInfo queryByTime(Long studyStartTime, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ZccmClassConfig> classConfigs = this.classConfigMapper.queryByTime(studyStartTime);
        List<ZccmClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

    private List<ZccmClassConfig> queryByAreaIdGetName(List<ZccmClassConfig> classConfigs) {
        if (CollectionUtils.isEmpty(classConfigs)) {
            throw new ResponseException(-1, "暂无培训班上线");
        }
        classConfigs.forEach(classConfig -> {
            ZccmAreaDistrict areaDistrict =
                    areaDistrictMapper.selectByPrimaryKey(classConfig.getAreaId());
            if (Objects.isNull(areaDistrict)) {
                return;
            }
            classConfig.setAreaName(areaDistrict.getAreaName());
        });
        return classConfigs;
    }


}