package com.ruoyi.project.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.TpAreaParaSelectItem;
import com.ruoyi.project.business.domain.TpElevatorModel;
import com.ruoyi.project.business.domain.TpParaSelectItem;
import com.ruoyi.project.business.domain.vo.paramSelectItem.AreaElevatorModelParaVO;
import com.ruoyi.project.business.domain.vo.paramSelectItem.ElevatorModeParamlVo;
import com.ruoyi.project.business.domain.vo.paramSelectItem.TabsVo;
import com.ruoyi.project.business.mapper.TpAreaParaSelectItemMapper;
import com.ruoyi.project.business.mapper.TpElevatorModelMapper;
import com.ruoyi.project.business.mapper.TpParaSelectItemMapper;
import com.ruoyi.project.business.service.ITpAreaParaSelectItemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class TpAreaParaSelectItemServiceImpl implements ITpAreaParaSelectItemService
{
    @Resource
    private TpAreaParaSelectItemMapper tpAreaParaSelectItemMapper;
    @Resource
    private TpParaSelectItemMapper tpParaSelectItemMapper;
    @Resource
    private TpElevatorModelMapper tpElevatorModelMapper;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TpAreaParaSelectItem selectTpAreaParaSelectItemById(Long id)
    {
        return tpAreaParaSelectItemMapper.selectTpAreaParaSelectItemById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tpAreaParaSelectItem 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TpAreaParaSelectItem> selectTpAreaParaSelectItemList(TpAreaParaSelectItem tpAreaParaSelectItem)
    {
        return tpAreaParaSelectItemMapper.selectTpAreaParaSelectItemList(tpAreaParaSelectItem);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tpAreaParaSelectItem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTpAreaParaSelectItem(TpAreaParaSelectItem tpAreaParaSelectItem)
    {
        tpAreaParaSelectItem.setCreateTime(DateUtils.getNowDate());
        return tpAreaParaSelectItemMapper.insertTpAreaParaSelectItem(tpAreaParaSelectItem);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tpAreaParaSelectItem 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTpAreaParaSelectItem(TpAreaParaSelectItem tpAreaParaSelectItem)
    {
        tpAreaParaSelectItem.setUpdateTime(DateUtils.getNowDate());
        return tpAreaParaSelectItemMapper.updateTpAreaParaSelectItem(tpAreaParaSelectItem);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaParaSelectItemByIds(Long[] ids)
    {
        return tpAreaParaSelectItemMapper.deleteTpAreaParaSelectItemByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaParaSelectItemById(Long id)
    {
        return tpAreaParaSelectItemMapper.deleteTpAreaParaSelectItemById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateModelParamSelectItem(ElevatorModeParamlVo paramlVo) {
        //1。获取下拉数据用于匹配
        List<AreaElevatorModelParaVO> paraSelect = new ArrayList<>();
        List<TabsVo> tabs  = paramlVo.getTabsList();
        for (TabsVo tab :tabs){
            paraSelect.addAll(tab.getParaSelect());
        }

        //获取Select类型参数的Id
        List<Long> selectIdList = paraSelect.stream()
                .filter(it-> "02".equals(it.getInputType()) || "04".equals(it.getInputType()))
                .map(AreaElevatorModelParaVO::getParaId)
                .collect(Collectors.toList());
        //查询select参数的下拉数据
        //List<TpAreaParaSelectItem> allList = tpAreaParaSelectItemMapper.selectTpAreaParaSelectItemListByIds(selectIdList);
        List<TpParaSelectItem> allList = tpParaSelectItemMapper.selectTpParaSelectItemListByIds(selectIdList);
        if(ObjectUtils.isEmpty(allList)){
            // todo 待国际化
            return "系统数据异常";
        }
        List<Long> paraIds = new ArrayList<>();
        List<TpAreaParaSelectItem> addSelectList = new ArrayList<>();
        //2.循环实体找出需要修改的参数
        for(AreaElevatorModelParaVO item : paraSelect){
//            if(item.operationFlag == 2){
//
//            }
            //2是修改过的数据，直接删除后新增
            //3.设置删除ids
            paraIds.add(item.getParaId());
            //4.设置新增实体
            List<Long> newSelectIds = item.getParaAreaSelectList();
            for(Long itemId : newSelectIds){
                try {
                    TpParaSelectItem param = allList.stream().filter(c -> itemId.equals(c.getId())).findFirst().get();
                    if(!ObjectUtils.isEmpty(param)){
                        TpAreaParaSelectItem selectItem = new TpAreaParaSelectItem();
                        selectItem.setElevatorModelId(paramlVo.getId());
                        selectItem.setId(param.getId());
                        selectItem.setParaCode(item.getParaCode());
                        selectItem.setParaSelectItemId(param.getId());
                        selectItem.setParaSelectItemValue(param.getValue());
                        selectItem.setParaId(item.getParaId());
                        selectItem.setCreateBy(SecurityUtils.getUserId()+"");
                        selectItem.setCreateTime(new Date());
                        addSelectList.add(selectItem);
                    }
                }catch (Exception e){

                }
            }
        }
//        if(!ObjectUtils.isEmpty(paraIds)){
//            Long[] ids = paraIds.toArray(new Long[paraIds.size()]);
//            //删除
//            int i = tpAreaParaSelectItemMapper.deleteTpAreaParaSelectItemByParaIds(ids,paramlVo.getId());
//        }
        //全部删除
        tpAreaParaSelectItemMapper.removeTpAreaParaSelectItemByElevatorModelId(paramlVo.getId());
        if(!ObjectUtils.isEmpty(addSelectList)){
            //新增
            int j = tpAreaParaSelectItemMapper.InsertBySql(addSelectList);
        }
        //操作主表
        TpElevatorModel elevatorMode  = new TpElevatorModel();
        elevatorMode.setId(paramlVo.getId());
        elevatorMode.setUpdateTime(DateUtils.getNowDate());
        tpElevatorModelMapper.updateTpElevatorModel(elevatorMode);
        return "";
    }
}
