package com.dk.mdm.service.mst;

import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.model.pojo.mst.StaffPurview;
import com.dk.mdm.mapper.mst.StaffPurviewMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.common.model.pojo.mst.StaffRight;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StaffPurviewService extends BaseService<StaffPurview> {

	@Override
	public BaseMapper<StaffPurview> getRepository() {
		return staffPurviewMapper;
	}

	@Autowired
	private StaffPurviewMapper staffPurviewMapper;

	/**
	 * @desc   : 查询范围权限
	 * @author : 常皓宁
	 * @date   : 2024/4/2 14:46
	 */
	public ResponseResultVO<List<StaffPurview>> getPurview(String staffId) {
		return ResponseResultUtil.success(staffPurviewMapper.selectByCond(new StaffPurview().setStaffId(staffId)));
	}

	/**
	 * @desc : 获取员工权限
	 * @author : 姜永辉
	 * @date : 2024/2/26 10:36
	 */
	public ResponseResultVO<Map<String, Object>> getStaffPurview(Map<String, Object> param) {
		// 获取系统基础数据
		List<Map<String, Object>> list = staffPurviewMapper.getStaffPurview(param);
		Map<String, Object> objectObjectHashMap = new HashMap<>();
		objectObjectHashMap.put("list", list);
		return ResponseResultUtil.success(objectObjectHashMap);
	}


	/**
	 * @desc : 保存员工范围权限
	 * @author : 姜永辉
	 * @date : 2024/2/26 10:36
	 */
	@Transactional(rollbackFor = {Exception.class})
	public ResponseResultVO<?> saveStaffPurview(List<StaffPurview> staffPurviews) {
		staffPurviewMapper.insertOrUpdateCondition(staffPurviews);
		return ResponseResultUtil.success();
	}

	public ResponseResultVO<Boolean> delete(String id) {
		return  ResponseResultUtil.success(staffPurviewMapper.deleteById(new StaffRight().setStaffId(id)) > 0) ;
	}

	/**
	 * @desc   : 保存范围权限
	 * @author : 常皓宁
	 * @date   : 2024/4/3 14:45
	 */
	@Transactional(rollbackFor = {Exception.class})
	public ResponseResultVO savePurview(List<StaffPurview> staffPurviewList, String staffId) {
//		通过用户删除
		staffPurviewMapper.deleteByStaff(staffId);
		if (staffPurviewList.size() > 0) {
			//		保存
			staffPurviewMapper.insertOrUpdateCondition(staffPurviewList);
		}
		return ResponseResultUtil.success();
	}

}
