package com.zbf.system.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.zbf.common.annotation.DataScope;
import com.zbf.common.utils.DateUtils;
import com.zbf.system.domain.TOngoodsshelf;
import com.zbf.system.mapper.TOngoodsshelfMapper;
import com.zbf.system.service.ITBaseStorageService;
import com.zbf.system.service.ITOngoodsshelfService;

/**
 * 入库任务Service业务层处理
 *
 * @author tony
 * @date 2024-06-14
 */
@Service
public class TOngoodsshelfServiceImpl extends ServiceImpl<TOngoodsshelfMapper, TOngoodsshelf> implements ITOngoodsshelfService //修改
{
    @Autowired
    private TOngoodsshelfMapper tOngoodsshelfMapper;
    @Autowired
    private ITBaseStorageService baseStorageService;

    /**
     * 查询入库任务
     *
     * @param id 入库任务主键
     * @return 入库任务
     */
    @Override
    public TOngoodsshelf selectTOngoodsshelfById(String id)
    {
        return tOngoodsshelfMapper.selectTOngoodsshelfById(id);
    }

    /**
     * 查询入库任务列表
     *
     * @param tOngoodsshelf 入库任务
     * @return 入库任务
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<TOngoodsshelf> selectTOngoodsshelfList(TOngoodsshelf tOngoodsshelf)
    {
        // TBaseStorage baseStorage = new TBaseStorage();
        // baseStorage.setDeptId(tOngoodsshelf.getDeptId());
        // List<TBaseStorage> tBaseStorages = baseStorageService.selectTBaseStorageList(baseStorage);
        // tOngoodsshelf.setStorageList(tBaseStorages.stream().map(TBaseStorage::getStorageId).collect(Collectors.toList()));

        return tOngoodsshelfMapper.selectTOngoodsshelfList(tOngoodsshelf);
    }

    /**
     * 新增入库任务
     *
     * @param tOngoodsshelf 入库任务
     * @return 结果
     */
    @Override
    public int insertTOngoodsshelf(TOngoodsshelf tOngoodsshelf)
    {
        tOngoodsshelf.setCreateTime(DateUtils.getNowDate());
        return tOngoodsshelfMapper.insertTOngoodsshelf(tOngoodsshelf);
    }

    /**
     * 修改入库任务
     *
     * @param tOngoodsshelf 入库任务
     * @return 结果
     */
    @Override
    public int updateTOngoodsshelf(TOngoodsshelf tOngoodsshelf)
    {
                tOngoodsshelf.setUpdateTime(DateUtils.getNowDate());
        return tOngoodsshelfMapper.updateTOngoodsshelf(tOngoodsshelf);
    }

    /**
     * 批量删除入库任务
     *
     * @param ids 需要删除的入库任务主键
     * @return 结果
     */
    @Override
    public int deleteTOngoodsshelfByIds(String[] ids)
    {
        return tOngoodsshelfMapper.deleteTOngoodsshelfByIds(ids);
    }

    /**
     * 删除入库任务信息
     *
     * @param id 入库任务主键
     * @return 结果
     */
    @Override
    public int deleteTOngoodsshelfById(String id)
    {
        return tOngoodsshelfMapper.deleteTOngoodsshelfById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 入库任务主键
     * @return 结果
     */
    @Override
    public int disabledTOngoodsshelfByIds(String[] ids) {
        return tOngoodsshelfMapper.disabledTOngoodsshelfByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 入库任务主键
     * @return 结果
     */
    @Override
    public int enableTOngoodsshelfByIds(String[] ids) {
        return tOngoodsshelfMapper.enableTOngoodsshelfByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tOngoodsshelf 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TOngoodsshelf tOngoodsshelf) {
        return tOngoodsshelfMapper.updateTOngoodsshelf(tOngoodsshelf);
    }

    /**
     * 入库任务
     * @param s
     * @param statuses
     * @return
     */
    public Map<String, Object> createTaskData(String s, List<Integer> statuses) {
        QueryWrapper<TOngoodsshelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("in_status AS status", "SUM(acc_num) as count")
                .in("in_status", statuses)
                .groupBy("in_status");
        List<Map<String, Object>> maps = this.selectMaps(queryWrapper);
        Map<String, Object> taskData = new HashMap<>();
        taskData.put("product", s);
        List<String> dimensions = Arrays.asList("product", "待执行", "执行中", "完成");

        // 封装
        for (Map<String, Object> map : maps) {
            String status = map.get("status").toString();
            BigDecimal count = (BigDecimal) map.get("count");
            int value = count.intValue();
            taskData.put(dimensions.get(Integer.parseInt(status) + 1), value);
        }
        return taskData;
    }

    @Override
    public List<TOngoodsshelf> list(LambdaQueryWrapper<TOngoodsshelf> tOngoodsshelfLambdaQueryWrapper) {
        return tOngoodsshelfMapper.selectList(tOngoodsshelfLambdaQueryWrapper);
    }

    private List<Map<String, Object>> selectMaps(QueryWrapper<TOngoodsshelf> queryWrapper) {
        return tOngoodsshelfMapper.selectMaps(queryWrapper);
    }

}
