package com.zkqy.system.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import com.zkqy.common.utils.DateUtils;
import com.zkqy.common.utils.SecurityUtils;
import com.zkqy.system.entity.DragTableStyle;
import com.zkqy.system.entity.vo.DragTableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zkqy.system.mapper.DragTableStyleMapper;
import com.zkqy.system.service.IDragTableStyleService;

/**
 * 动态格样式Service业务层处理
 * 
 * @author zkqy
 * @date 2023-11-03
 */
@Service
public class DragTableStyleServiceImpl implements IDragTableStyleService 
{
    @Autowired
    private DragTableStyleMapper dragTableStyleMapper;

    /**
     * 查询动态格样式
     * 
     * @param id 动态格样式主键
     * @return 动态格样式
     */
    @Override
    public DragTableStyle selectDragTableStyleById(Long id)
    {
        return dragTableStyleMapper.selectDragTableStyleById(id);
    }

    /**
     * 查询动态格样式列表
     * 
     * @param dragTableStyle 动态格样式
     * @return 动态格样式
     */
    @Override
    public List<DragTableStyle> selectDragTableStyleList(DragTableStyle dragTableStyle)
    {
        return dragTableStyleMapper.selectDragTableStyleList(dragTableStyle);
    }

    /**
     * 新增动态格样式
     * 
     * @param vo 动态格样式
     * @return 结果
     */
    @Override
    public int batchInsertDragTableStyle(DragTableVo vo)
    {
        vo.getDragTableStyleList().forEach(v -> {v.setTableKey(vo.getTableKey());});
        return dragTableStyleMapper.batchInsertDragTableStyle(vo.getDragTableStyleList());
    }

    /**
     * 修改动态格样式
     * 
     * @param vo 动态格样式
     * @return 结果
     */
    @Override
    public void updateDragTableStyle(DragTableVo vo)
    {
        LinkedList<DragTableStyle> addList = new LinkedList<>();
        LinkedList<DragTableStyle> editList = new LinkedList<>();
        List<Long> numList = new ArrayList<>();
        //判断新增还是修改
        vo.getDragTableStyleList().stream().forEach(s -> {
            if (s.getId() == null){
                s.setTableKey(vo.getTableKey());
                s.setCreateBy(SecurityUtils.getUsername());
                s.setCreateById(SecurityUtils.getUserId());
                s.setCreateTime(DateUtils.getNowDate());
                addList.add(s);
            }else {
                s.setUpdateBy(SecurityUtils.getUsername());
                s.setUpdateById(SecurityUtils.getUserId());
                s.setUpdateTime(DateUtils.getNowDate());
                editList.add(s);
            }
            numList.add(s.getId());
        });
        //查询动态表格样式编号
        List<Long> allIds = dragTableStyleMapper.selectIdByTableKey(vo.getTableKey());
        allIds.removeAll(numList);
        //删除动态表格样式
        if(allIds.size() > 0){
            dragTableStyleMapper.deleteDragTableStyleByIds(allIds);
        }
        if(addList.size() > 0){
            dragTableStyleMapper.batchInsertDragTableStyle(addList);
        }
        if(editList.size() >0){
            editList.stream().forEach(e -> dragTableStyleMapper.updateDragTableStyle(e));
        }
    }

    /**
     * 批量删除动态格样式
     * 
     * @param ids 需要删除的动态格样式主键
     * @return 结果
     */
    @Override
    public int deleteDragTableStyleByIds(List<Long> ids)
    {
        return dragTableStyleMapper.deleteDragTableStyleByIds(ids);
    }

    @Override
    public int deleteDragTableStyleByTableKeys(List<String> tableKeys)
    {
        return dragTableStyleMapper.deleteDragTableStyleByTableKeys(tableKeys);
    }
}
