package com.ruoyi.system.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.CommonEntity;
import com.ruoyi.system.entity.DragTableCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DragTableConditionMapper;
import com.ruoyi.system.service.IDragTableConditionService;

/**
 * 动态表格条件Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class DragTableConditionServiceImpl implements IDragTableConditionService 
{
    @Autowired
    private DragTableConditionMapper dragTableConditionMapper;


    @Override
    public List<CommonEntity> selectDragTableConditionList(CommonEntity commonEntity)
    {
        Map<String, Object> conditions = JSONObject.parseObject(JSON.toJSONString(commonEntity.getQueryMap()));
        return dragTableConditionMapper.selectDragTableConditionList(conditions);
    }

    @Override
    public int insertDragTableCondition(CommonEntity commonEntity)
    {
        List<Object> list = (List<Object>) commonEntity.getCommMap();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Object obj : list) {
            System.out.println(obj);
            Map<String, Object> map1 = (Map<String, Object>) obj;
            mapList.add(map1);
        }
        Set<String> fieldNames = new LinkedHashSet<>();
        mapList.stream().forEach(map1 -> {
            map1.forEach((key, value) -> {
                fieldNames.add(key);
            });
        });
        return dragTableConditionMapper.insertDragTableCondition(fieldNames, mapList);
    }

    @Override
    public int updateDragTableCondition(CommonEntity commonEntity)
    {
        Map<String, Object> conditions = JSONObject.parseObject(JSON.toJSONString(commonEntity.getConditionMap()));
        Map<String, Object> fields = JSONObject.parseObject(JSON.toJSONString(commonEntity.getCommMap()));
        return dragTableConditionMapper.updateDragTableCondition(fields, conditions);
    }

    @Override
    public int deleteDragTableConditionByTcIds(CommonEntity commonEntity)
    {
        Map<String, Object> conditions = JSONObject.parseObject(JSON.toJSONString(commonEntity.getConditionMap()));
        return dragTableConditionMapper.deleteDragTableConditionByTcIds(conditions);
    }


}
