package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ClassPrintDto;
import com.hc.business.mapper.AreaDistrictMapper;
import com.hc.business.mapper.ClassConfigMapper;
import com.hc.business.mapper.UserMapper;
import com.hc.business.model.AreaDistrict;
import com.hc.business.model.ClassConfig;
import com.hc.business.service.ClassConfigService;
import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.tool.status.CodeFlagEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

@Service("classConfigService")
public class ClassConfigServiceImpl implements ClassConfigService {

    private final ClassConfigMapper classConfigMapper;

    private final AreaDistrictMapper areaDistrictMapper;

    private final UserMapper userMapper;

    public ClassConfigServiceImpl(ClassConfigMapper classConfigMapper, AreaDistrictMapper areaDistrictMapper, UserMapper userMapper) {
        this.classConfigMapper = classConfigMapper;
        this.areaDistrictMapper = areaDistrictMapper;
        this.userMapper = userMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param classConfig 实例对象
     * @return 实例对象
     */
    @Override
    public ClassConfig update(ClassConfig 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<ClassConfig> classConfigs = this.classConfigMapper.queryAll();
        List<ClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

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

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

    @Override
    public PageInfo queryByAreaId(Integer areaId, int page, int size) {
        PageHelper.startPage(page, size);
        List<ClassConfig> classConfigs = this.classConfigMapper.queryByAreaId(areaId);

        //add 如果查询数据是空，默认直接显示北京培训班
        if((null  == classConfigs) || (true == classConfigs.isEmpty())){
            classConfigs = this.classConfigMapper.queryByAreaId(1);
        }

        List<ClassConfig> classConfigsList = queryByAreaIdGetName(classConfigs);
        return new PageInfo(classConfigsList);
    }

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

    @Override
    public PageInfo queryByTarget(String target, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ClassConfig> classConfigs = this.classConfigMapper.queryByTarget(target);
        return new PageInfo(classConfigs);
    }

    @Override
    public PageInfo queryByYear(String year, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ClassConfig> classConfigs = this.classConfigMapper.queryByYear(year);
        return new PageInfo(classConfigs);
    }

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

    @Override
    public List<ClassPrintDto> getPrintList(String cardId) {
        List<ClassPrintDto> printList = this.classConfigMapper.getPrintList(cardId);
        for (ClassPrintDto model:printList)
        {
            model.setClassConfigModel(this.classConfigMapper.selectByPrimaryKey(model.getClassId()));
            model.setUserModel(this.userMapper.selectByPrimaryKey(model.getUserId()));
        }
        return printList;
    }

}