package com.pnkx.service.impl;

import com.pnkx.common.utils.DateUtils;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.domain.po.PxBookkeepingClassification;
import com.pnkx.mapper.PxBookkeepingClassificationMapper;
import com.pnkx.mapper.PxBookkeepingRecordMapper;
import com.pnkx.service.IPxBookkeepingClassificationService;
import org.springframework.stereotype.Service;

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

@Service
public class PxBookkeepingClassificationServiceImpl implements IPxBookkeepingClassificationService {
    @Resource
    private PxBookkeepingClassificationMapper pxBookkeepingClassificationMapper;
    @Resource
    PxBookkeepingRecordMapper pxBookkeepingRecordMapper;

    /**
     * 查询账本分类
     *
     * @param id 账本分类ID
     * @return 账本分类
     */
    @Override
    public PxBookkeepingClassification selectPxBookkeepingClassificationById(Long id) {
        return pxBookkeepingClassificationMapper.selectPxBookkeepingClassificationById(id);
    }

    /**
     * 查询账本分类列表
     *
     * @param pxBookkeepingClassification 账本分类
     * @return 账本分类
     */
    @Override
    public List<PxBookkeepingClassification> selectPxBookkeepingClassificationList(PxBookkeepingClassification pxBookkeepingClassification) {
        return pxBookkeepingClassificationMapper.selectPxBookkeepingClassificationList(pxBookkeepingClassification);
    }

    /**
     * 新增账本分类
     *
     * @param pxBookkeepingClassification 账本分类
     * @return 结果
     */
    @Override
    public int insertPxBookkeepingClassification(PxBookkeepingClassification pxBookkeepingClassification) {
        pxBookkeepingClassification.setCreateTime(DateUtils.getNowDate());
        pxBookkeepingClassification.setCreateBy(SecurityUtils.getUserId().toString());
        return pxBookkeepingClassificationMapper.insertPxBookkeepingClassification(pxBookkeepingClassification);
    }

    /**
     * 修改账本分类
     *
     * @param pxBookkeepingClassification 账本分类
     * @return 结果
     */
    @Override
    public int updatePxBookkeepingClassification(PxBookkeepingClassification pxBookkeepingClassification) {
        pxBookkeepingClassification.setUpdateTime(DateUtils.getNowDate());
        pxBookkeepingClassification.setUpdateBy(SecurityUtils.getUserId().toString());
        return pxBookkeepingClassificationMapper.updatePxBookkeepingClassification(pxBookkeepingClassification);
    }

    /**
     * 批量删除账本分类
     *
     * @param ids 需要删除的账本分类ID
     * @return 结果
     */
    @Override
    public int deletePxBookkeepingClassificationByIds(Long[] ids) {
        pxBookkeepingRecordMapper.deletePxBookkeepingRecordByTypes(ids);
        return pxBookkeepingClassificationMapper.deletePxBookkeepingClassificationByIds(ids);
    }

    /**
     * 删除账本分类信息
     *
     * @param id 账本分类ID
     * @return 结果
     */
    @Override
    public int deletePxBookkeepingClassificationById(Long id) {
        PxBookkeepingClassification pxBookkeepingClassification = pxBookkeepingClassificationMapper.selectPxBookkeepingClassificationById(id);
        if ("0".equals(pxBookkeepingClassification.getTypeLevel())) {
            List<Long> typeList = pxBookkeepingClassificationMapper.getTypeListByParentId(id);
            Long[] typeArray = typeList.toArray(new Long[0]);
            pxBookkeepingClassificationMapper.deletePxBookkeepingClassificationByIds(typeArray);
            pxBookkeepingRecordMapper.deletePxBookkeepingRecordByTypes(typeArray);
        }
        return pxBookkeepingClassificationMapper.deletePxBookkeepingClassificationById(id);
    }

    /**
     * 查询最近使用分类列表
     *
     * @param pxBookkeepingClassification 分类
     * @return 分类列表
     */
    @Override
    public List<PxBookkeepingClassification> getLatelyTypeList(PxBookkeepingClassification pxBookkeepingClassification) {
        List<PxBookkeepingClassification> result = new ArrayList<>();
        List<PxBookkeepingClassification> list = pxBookkeepingClassificationMapper.getLatelyTypeList(pxBookkeepingClassification);
        list.forEach(item -> {
            if (!result.contains(item)) {
                result.add(item);
            }
        });
        if (result.size() > 6) {
            return result.subList(0, 6);
        }
        return result;
    }
}
