package com.kcim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.exception.CostException;
import com.kcim.common.util.UserContext;
import com.kcim.common.constants.NumberConstant;
import com.kcim.dao.mapper.CostResponsibilityDepartmentMapper;
import com.kcim.dao.model.CostResponsibilityDepartment;
import com.kcim.dao.model.Department;
import com.kcim.dao.repository.CostResponsibilityDepartmentRepository;
import com.kcim.vo.CostDepartmentVO;
import com.kcim.service.CostResponsibilityDepartmentService;
import com.kcim.common.util.BeanUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("/costResponsibilityDepartmentService")
@AllArgsConstructor
public class CostResponsibilityDepartmentServiceImpl extends ServiceImpl<CostResponsibilityDepartmentMapper, CostResponsibilityDepartment> implements CostResponsibilityDepartmentService {

    private DepartmentServiceImpl departmentService;

    CostResponsibilityDepartmentRepository repository;
    /**
     * 根据责任中心的Id获取对应的科室信息的集合
     *
     * @param responsibilityId
     * @return
     */
    @Override
    public List<CostDepartmentVO> getByResponsibilityId(Integer responsibilityId) {
        if (responsibilityId<=0){
            throw new CostException("请选择责任中心数据");
        }

        List<CostResponsibilityDepartment> list = repository.getList();

        // 当前医院责任中心所具有的的科室
        List<CostResponsibilityDepartment> costResponsibilityDepartmentList = baseMapper.selectList(new QueryWrapper<CostResponsibilityDepartment>().lambda().eq(CostResponsibilityDepartment::getResponsibilityId, responsibilityId));
        Map<Long, List<CostResponsibilityDepartment>> costResponsibilityDepartmentMap = costResponsibilityDepartmentList.stream().distinct().collect(Collectors.groupingBy(CostResponsibilityDepartment::getDepartmentId));
        // 所有科室
        List<Department> departmentList = departmentService.getByDepartment(null,null,null, UserContext.getHospId());
        if(!CollectionUtils.isEmpty(list)){
            //过滤出已对照过的科室
            List<Long> collect = list.stream().map(CostResponsibilityDepartment::getDepartmentId).collect(Collectors.toList());
            List<Department> removeList = new ArrayList<>();
            for (Department department : departmentList) {
                if(collect.contains(department.getId())){
                    removeList.add(department);
                }
            }
            if(!CollectionUtils.isEmpty(removeList)){
                departmentList.removeAll(removeList);
            }
        }




        List<CostDepartmentVO> departVOList = BeanUtil.convertList(departmentList, CostDepartmentVO.class);
        departVOList.forEach(i->{
            if (!CollectionUtils.isEmpty(costResponsibilityDepartmentMap.get(i.getId()))){
                i.setDepartmentStatus(NumberConstant.ONE);
            }else {
                i.setDepartmentStatus(NumberConstant.ZERO);
            }
        });
        return departVOList;
    }

    /**
     * 批量删除责任中心科室损益信息
     *
     * @param idList 责任中心科室对应的Id集合
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteByIds(List<Integer> idList) {
        this.removeByIds(idList);
    }
}