package com.ho.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.ho.business.entity.Station;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.PageVO;
import com.ho.common.tools.entity.SysPermission;
import com.ho.common.tools.entity.SysRole;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageUtilsOld;
import com.ho.user.api.entity.PermStationRelation;
import com.ho.user.feignclient.BusinessFeignClient;
import com.ho.user.mapper.PermStationRelationMapper;
import com.ho.user.mapper.SysPermissionMapper;
import com.ho.user.service.PermStationRelationService;
import com.ho.user.vo.req.StationPermissionReqVo;
import com.ho.user.vo.resp.StationPermissionRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @TableName perm_station_relation
 */
@Service
@Slf4j
public class PermStationRelationImpl implements PermStationRelationService {
    @Autowired
    PermStationRelationMapper permStationRelationMapper;

    @Autowired
    SysPermissionMapper sysPermissionMapper;


    @Override
    public int insertSelective(PermStationRelation record) {
        if (!record.getPermissions().isEmpty()) {
            //新增电站与菜单的关联数据
            //关联表自身进行数据新增
            for (Integer permissionId : record.getPermissions()) {

                PermStationRelation permStationRelation = new PermStationRelation();
                permStationRelation.setStationId(record.getStationId());
                permStationRelation.setPermissionId(permissionId);
                Date date = new Date();
                SimpleDateFormat fmt = new SimpleDateFormat(CommonConstant.DATE);
                String dateStr = fmt.format(date);
                permStationRelation.setCreateTime(dateStr);
                permStationRelation.setStationName(record.getStationName());
                permStationRelationMapper.insertSelective(permStationRelation);
            }
        }
        return permStationRelationMapper.insertSelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return permStationRelationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PermStationRelation record) {
        return permStationRelationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PermStationRelation record) {
        return permStationRelationMapper.updateByPrimaryKey(record);
    }

    @Override
    public void update(PermStationRelation record) {
        if (record.getPermissions() != null && !record.getPermissions().isEmpty()) {
            //不为空时，将该菜单关联的电站全部删除后再重新增加
            //根据菜单Id
            List<PermStationRelation> permStationRelations = selectByStationId(record.getStationId());
            //删除
            if (!permStationRelations.isEmpty()) {
                List<Integer> idList = permStationRelations.stream().map(s -> {
                            return s.getId();
                        }
                ).collect(Collectors.toList());
                permStationRelationMapper.deleteBatch(idList);
            }

            //添加
            //关联表自身进行数据新增
            for (Integer permissionId : record.getPermissions()) {
                PermStationRelation permStationRelation = new PermStationRelation();
                permStationRelation.setStationId(record.getStationId());
                permStationRelation.setPermissionId(permissionId);
                permStationRelation.setStationName(record.getStationName());
                Date date = new Date();
                SimpleDateFormat fmt = new SimpleDateFormat(CommonConstant.DATE);
                String dateStr = fmt.format(date);
                permStationRelation.setCreateTime(dateStr);
                permStationRelationMapper.insertSelective(permStationRelation);
            }
        } else {
            //在更改时，判断电站list是否为空，为空的话就将更为公共，再根据菜单Id找到关联表，检查是否有数据，有则删掉
            //根据电站Id
            List<PermStationRelation> permStationRelations = selectByStationId(record.getStationId());
            if (!permStationRelations.isEmpty()) {
                List<Integer> idList = permStationRelations.stream().map(s -> {
                            return s.getId();
                        }
                ).collect(Collectors.toList());
                deleteBatch(idList);
            }
        }
    }

    @Override
    public List<PermStationRelation> selectByStationId(Integer stationId) {
        return permStationRelationMapper.selectByStationId(stationId);
    }

    @Override
    public List<PermStationRelation> selectByPermissionId(Integer permissionId) {
        return permStationRelationMapper.selectByPermissionId(permissionId);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        permStationRelationMapper.deleteBatchStationId(ids);
    }


    @Override
    public PermStationRelation detailInfo(Integer id) {
        PermStationRelation permStationRelation = new PermStationRelation();
        //入参电站Id 查询关联表 ，返回菜单数据
        List<PermStationRelation> permStationRelations = selectByStationId(id);
        List<Integer> collect = permStationRelations.stream().map(s -> {
                    return s.getPermissionId();
                }
        ).collect(Collectors.toList());
        permStationRelation.setPermissions(collect);
        permStationRelation.setStationId(id);
        return permStationRelation;
    }

}