package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ActSupplies;
import com.ruoyi.system.mapper.ActSuppliesMapper;
import com.ruoyi.system.service.IActSuppliesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 物料总分类管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-11
 */
@Service
public class ActSuppliesServiceImpl implements IActSuppliesService
{
    @Autowired
    private ActSuppliesMapper actSuppliesMapper;

    /**
     * 查询物料总分类管理
     *
     * @param id 物料总分类管理ID
     * @return 物料总分类管理
     */
    @Override
    public ActSupplies selectActSuppliesById(Long id)
    {
        return actSuppliesMapper.selectActSuppliesById(id);
    }

    /**
     * 查询物料总分类管理列表
     *
     * @param actSupplies 物料总分类管理
     * @return 物料总分类管理
     */
    @Override
    public List<ActSupplies> selectActSuppliesList(ActSupplies actSupplies)
    {
        return actSuppliesMapper.selectActSuppliesList(actSupplies);
    }

    /**
     * 新增物料总分类管理
     *
     * @param actSupplies 物料总分类管理
     * @return 结果
     */
    @Override
    public int insertActSupplies(ActSupplies actSupplies)
    {

        int countBySuppliesName = actSuppliesMapper.countBySuppliesName(actSupplies);

        if (actSupplies.getLevel()==2){
            actSupplies.setTwoLevel(actSupplies.getOneLevel()+actSupplies.getTwoLevel());
        }else if (actSupplies.getLevel()==3){
            actSupplies.setThreeLevel(actSupplies.getTwoLevel()+actSupplies.getThreeLevel());
        }

        int countBySuppliesLevel = actSuppliesMapper.countBySuppliesLevel(actSupplies);

        if (countBySuppliesLevel>0){
            return -1;
        }

        if(countBySuppliesName > 0){
            return 0;
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSupplies.setCreateBy(user.getUserName());
        actSupplies.setCreateTime(DateUtils.getNowDate());

        return actSuppliesMapper.insertActSupplies(actSupplies);
    }

    /**
     * 修改物料总分类管理
     *
     * @param actSupplies 物料总分类管理
     * @return 结果
     */
    @Override
    public int updateActSupplies(ActSupplies actSupplies)
    {
        int countBySuppliesName = actSuppliesMapper.countBySuppliesName(actSupplies);
        if(countBySuppliesName > 0) {
            return 0;
        }

        if (actSupplies.getLevel()==2){
            actSupplies.setTwoLevel(actSupplies.getOneLevel()+actSupplies.getTwoLevel());
        }else if (actSupplies.getLevel()==3){
            actSupplies.setThreeLevel(actSupplies.getTwoLevel()+actSupplies.getThreeLevel());
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSupplies.setUpdateBy(user.getUserName());
        actSupplies.setUpdateTime(DateUtils.getNowDate());

        return actSuppliesMapper.updateActSupplies(actSupplies);
    }

    /**
     * 批量删除物料总分类管理
     *
     * @param ids 需要删除的物料总分类管理ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesByIds(Long[] ids)
    {
        List<Integer> findall = actSuppliesMapper.findall(ids);
        List<ActSupplies> allId = actSuppliesMapper.findAllId(ids);

        Map<Long,Long> map = new HashMap<>();

        for (int i=0;i<allId.size();i++){
            Long allIds = actSuppliesMapper.findAllIds(allId.get(i));
            map.put(allId.get(i).getId(),allIds);
        }

        for (Long key : map.keySet()){

            if (map.get(key)>1){
                return 0;
            }

        }

        if (findall.size()>0){
            return 0;
        }

        return actSuppliesMapper.deleteActSuppliesByIds(ids);
    }

    /**
     * 删除物料总分类管理信息
     *
     * @param id 物料总分类管理ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesById(Long id)
    {
        return actSuppliesMapper.deleteActSuppliesById(id);
    }

    @Override
    public List<ActSupplies> options() {
        return actSuppliesMapper.options();
    }

    public int setEnable(Long id, Integer status) {
        updateStatusById(id, status);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);
        return actSuppliesMapper.setEnable(paramMap);
    }

    public int setDisable(Long id, Integer status) {
        updateStatusById(id, status);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);
        return actSuppliesMapper.setDisable(paramMap);
    }

    @Override
    public List<Integer> find(Long id) {
        return actSuppliesMapper.find(id);
    }

    @Override
    public List<Integer> findId(Long id) {
        return actSuppliesMapper.findId(id);
    }

    private int updateStatusById(Long id, Integer status){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);
        return actSuppliesMapper.updateActSupplies(paramMap);
    }

}

