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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.business.domain.TpAreaParaStandard;
import com.ruoyi.project.business.domain.TpElevatorModel;
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.TpAreaParaStandardMapper;
import com.ruoyi.project.business.mapper.TpElevatorModelMapper;
import com.ruoyi.project.business.service.ITpAreaParaStandardService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class TpAreaParaStandardServiceImpl implements ITpAreaParaStandardService
{
    @Autowired
    private TpAreaParaStandardMapper tpAreaParaStandardMapper;
    @Resource
    private TpElevatorModelMapper tpElevatorModelMapper;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TpAreaParaStandard selectTpAreaParaStandardById(Long id)
    {
        return tpAreaParaStandardMapper.selectTpAreaParaStandardById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tpAreaParaStandard 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TpAreaParaStandard> selectTpAreaParaStandardList(TpAreaParaStandard tpAreaParaStandard)
    {
        return tpAreaParaStandardMapper.selectTpAreaParaStandardList(tpAreaParaStandard);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tpAreaParaStandard 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTpAreaParaStandard(TpAreaParaStandard tpAreaParaStandard)
    {
        tpAreaParaStandard.setCreateTime(DateUtils.getNowDate());
        return tpAreaParaStandardMapper.insertTpAreaParaStandard(tpAreaParaStandard);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tpAreaParaStandard 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTpAreaParaStandard(TpAreaParaStandard tpAreaParaStandard)
    {
        tpAreaParaStandard.setUpdateTime(DateUtils.getNowDate());
        return tpAreaParaStandardMapper.updateTpAreaParaStandard(tpAreaParaStandard);
    }

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

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateModelParamStandard(ElevatorModeParamlVo paramlVo) {
        //1.直接获取所有的参数数据
        List<AreaElevatorModelParaVO> paraSelect = new ArrayList<>();
        List<TabsVo> tabs  = paramlVo.getTabsList();
        for (TabsVo tab :tabs){
            paraSelect.addAll(tab.getParaSelect());
        }
        List<TpAreaParaStandard> standList = new ArrayList<>();
        for(AreaElevatorModelParaVO standard : paraSelect){
            //全部删不了然后重新插入
//            if(standard.operationFlag == 2){
//                //代表进行了修改操作
//                //批量进行update
//                TpAreaParaStandard upd = new  TpAreaParaStandard();
//                upd.setId(standard.getStandardId());
//                upd.setParaDefaultId(standard.getParaDefaultId());
//                upd.setParaDefaultValue(standard.getParaDefaultValue());
//
//                upd.setUpdateTime(new Date());
//                upd.setUpdateBy(SecurityUtils.getUserId()+"");
//                if(!ObjectUtils.isEmpty(upd.getId())){
//                    standList.add(upd);
//                }
//            }
            //设置默认值的实体
            TpAreaParaStandard standardIns = new TpAreaParaStandard();
            standardIns.setElevatorModelId(paramlVo.getId());
            standardIns.setParaId(standard.getParaId());
            standardIns.setParaCode(standard.getParaCode());
            standardIns.setParaDefaultValue(standard.getParaDefaultValue());
            //如果存在默认值，并且其是选项
            if("02".equals(standard.getInputType()) || "04".equals(standard.getInputType())){
                //则需额外设定id
                standardIns.setParaDefaultId(standard.getParaDefaultId());
            }
            standardIns.setCreateBy(SecurityUtils.getUserId()+"");
            standardIns.setCreateTime(new Date());
            standList.add(standardIns);
        }
        tpAreaParaStandardMapper.removeTpAreaParaStandardByElevatorModelId(paramlVo.getId());
        if(!ObjectUtils.isEmpty(standList)){
            int i = tpAreaParaStandardMapper.InsertBySql(standList);
        }
        //操作主表
        TpElevatorModel elevatorMode  = new TpElevatorModel();
        elevatorMode.setId(paramlVo.getId());
        elevatorMode.setUpdateTime(DateUtils.getNowDate());
        tpElevatorModelMapper.updateTpElevatorModel(elevatorMode);
        return "";
    }
}
