package com.ruoyi.interfaces.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.domain.SysCate;
import com.ruoyi.interfaces.mapper.SysCateMapper;
import com.ruoyi.interfaces.service.SysCateService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017-09-14.
 */
@Service
public class SysCateServiceImpl implements SysCateService {

    private final SysCateMapper sysCateMapper;

    public SysCateServiceImpl(SysCateMapper sysCateMapper) {
        this.sysCateMapper = sysCateMapper;
    }

    /**
     * 插入系统分类信息
     *
     * @param sysCate
     */
    @Override
    public int insert(SysCate sysCate) {
        sysCate.setCateCode(getCateCode(sysCate.getCatePcode()));
        return sysCateMapper.insertSelective(sysCate);
    }

    private String getCateCode(String pcode) {
        String code = pcode;
        List<SysCate> cateList = sysCateMapper.listByPcode(pcode);
        if (CollectionUtils.isEmpty(cateList)) {
            return code + "01";
        }
        int number = cateList.stream()
                .map(SysCate::getCateCode)
                .map(c -> Integer.parseInt(c.replace(pcode, "")))
                .max(Integer::compare)
                .orElse(0);
        if (++number < 10) {
            code = code + "0" + number;
        } else {
            code = code + number;
        }
        return code;
    }

    @Override
    public int update(SysCate sysCate) {
        return sysCateMapper.updateByPrimaryKeySelective(sysCate);
    }

    @Override
    public int delete(String id) {
        return sysCateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SysCate get(String id) {
        return sysCateMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysCate> countNum() {
        return sysCateMapper.countNum(null);
    }

    @Override
    public List<SysCate> list(SysCate sysCate) {
        return sysCateMapper.findAll(sysCate);
    }

    @Override
    public PageInfo<SysCate> pageList(PageParam<SysCate> sysCatePageParam) {
        PageHelper.startPage(sysCatePageParam);
        List<SysCate> sysCateList = sysCateMapper.findAll(sysCatePageParam.getData());
        return new PageInfo<>(sysCateList);
    }

    /**
     * 查询所有系统分类信息
     *
     * @return
     */
    @Override
    public List<SysCate> findAll(SysCate sysCate) {
        return sysCateMapper.findAll(sysCate);
    }

    /**
     * 查询所有系统分类信息
     *
     * @return
     */
    @Override
    public List<SysCate> findAllO(SysCate sysCate) {
        return sysCateMapper.findAllO(sysCate);
    }

    /**
     * 根据分类编码查询系统分类信息
     *
     * @param cateCode
     * @return
     */
    @Override
    public SysCate getCateById(String cateCode) {
        return sysCateMapper.selectByPrimaryKey(cateCode);
    }

    /**
     * 根据分类编码修改系统分类信息
     *
     * @param sysCate
     */
    @Override
    public int updateCateById(SysCate sysCate) {
        return sysCateMapper.updateByPrimaryKeySelective(sysCate);
    }

    /**
     * 根据分类编码删除系统分类信息
     *
     * @param cateCode
     */
    @Override
    public int deleteCateById(String cateCode) {
        return sysCateMapper.deleteByPrimaryKey(cateCode);
    }

    /**
     * 修改系统分类状态
     *
     * @param cate
     * @return
     */
    @Override
    public int updateStatus(SysCate cate) {
        return sysCateMapper.updateStatus(cate);
    }

    @Override
    public int getCount() {
        return sysCateMapper.getCount();
    }

    @Override
    public List<SysCate> listWithCount() {
        return sysCateMapper.countNum("1");
    }


}
