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.domain.ActSuppliesDetails;
import com.ruoyi.system.mapper.ActSuppliesDetailsMapper;
import com.ruoyi.system.service.IActSuppliesDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 物料细分类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-11
 */
@Service
public class ActSuppliesDetailsServiceImpl implements IActSuppliesDetailsService
{
    @Autowired
    private ActSuppliesDetailsMapper actSuppliesDetailsMapper;

    /**
     * 查询物料细分类管理
     * 
     * @param id 物料细分类管理ID
     * @return 物料细分类管理
     */
    @Override
    public ActSuppliesDetails selectActSuppliesDetailsById(Long id)
    {
        return actSuppliesDetailsMapper.selectActSuppliesDetailsById(id);
    }

    /**
     * 查询物料细分类管理列表
     * 
     * @param actSuppliesDetails 物料细分类管理
     * @return 物料细分类管理
     */
    @Override
    public List<ActSuppliesDetails> selectActSuppliesDetailsList(ActSuppliesDetails actSuppliesDetails)
    {
        return actSuppliesDetailsMapper.selectActSuppliesDetailsList(actSuppliesDetails);
    }

    @Override
    public List<ActSuppliesDetails> selectActSuppliesDetailsMaterialList(ActSuppliesDetails actSuppliesDetails)
    {
        return actSuppliesDetailsMapper.selectActSuppliesDetailsMaterialList(actSuppliesDetails);
    }

    /**
     * 新增物料细分类管理
     * 
     * @param actSuppliesDetails 物料细分类管理
     * @return 结果
     */
    @Override
    public int insertActSuppliesDetails(ActSuppliesDetails actSuppliesDetails)
    {

        int countByDetailsName = actSuppliesDetailsMapper.countByDetailsName(actSuppliesDetails);
        if(countByDetailsName > 0){
            return 0;
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesDetails.setCreateBy(user.getUserName());
        actSuppliesDetails.setCreateTime(DateUtils.getNowDate());

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

        int countBySuppliesLevel = actSuppliesDetailsMapper.countBySuppliesLevel(actSuppliesDetails);

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

        return actSuppliesDetailsMapper.insertActSuppliesDetails(actSuppliesDetails);
    }


    /**
     * 修改物料细分类管理
     * 
     * @param actSuppliesDetails 物料细分类管理
     * @return 结果
     */
    @Override
    public int updateActSuppliesDetails(ActSuppliesDetails actSuppliesDetails)
    {
        int countByDetailsName = actSuppliesDetailsMapper.countByDetailsName(actSuppliesDetails);
        if(countByDetailsName > 0){
            return 0;
        }

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

        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesDetails.setUpdateBy(user.getUserName());
        actSuppliesDetails.setCreateTime(DateUtils.getNowDate());

        return actSuppliesDetailsMapper.updateActSuppliesDetails(actSuppliesDetails);
    }

    /**
     * 批量删除物料细分类管理
     * 
     * @param ids 需要删除的物料细分类管理ID
     * @return 结果
     */
    @Override
    public List<Long> deleteActSuppliesDetailsByIds(Long[] ids)
    {
        List<Long> idsl = new ArrayList<>();
        List<ActSuppliesDetails> actSuppliesDetails = actSuppliesDetailsMapper.findAllId(ids);

        for (int i=0;i<actSuppliesDetails.size();i++){
            Long allIds = actSuppliesDetailsMapper.findAllIds(actSuppliesDetails.get(i));
            if (allIds.intValue()>1){

                idsl.add(actSuppliesDetails.get(i).getId());

            }else {

                actSuppliesDetailsMapper.deleteActSuppliesDetailsByIds(ids);

            }

        }

        return idsl;

    }

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

    @Override
    public List<ActSupplies> saveSid() {
        return actSuppliesDetailsMapper.saveSid();
    }
    @Override
    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 actSuppliesDetailsMapper.setEnable(paramMap);
    }

    @Override
    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 actSuppliesDetailsMapper.setDisable(paramMap);
    }

    @Override
    public List<ActSuppliesDetails> getLevel() {
        return actSuppliesDetailsMapper.getLevel();
    }

    @Override
    public List<ActSuppliesDetails> selectActSuppliesDetailsMaterialListPZ(ActSuppliesDetails actSuppliesDetails) {
        return actSuppliesDetailsMapper.selectActSuppliesDetailsMaterialListPZ(actSuppliesDetails);
    }


    private int updateStatusById(Long id, Integer status){

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);

        return actSuppliesDetailsMapper.updateActSuppliesDetails(paramMap);

    }
}
