package com.casic.missiles.service.Impl.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.mapper.business.BusinessSiteExecutiveStaffRelationMapper;
import com.casic.missiles.mapper.meter.MeterStaffMapper;
import com.casic.missiles.model.business.BusinessSiteExecutiveStaffRelation;
import com.casic.missiles.model.meter.MeterStaff;
import com.casic.missiles.modular.system.model.Dept;
import com.casic.missiles.modular.system.service.IDeptService;
import com.casic.missiles.service.business.IBusinessSiteExecutiveStaffRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 外场检测-计量人员关联表 服务实现类
 * </p>
 *
 * @author zt
 * @since 2023-03-23
 */
@Service
public class BusinessSiteExecutiveStaffRelationServiceImpl extends ServiceImpl<BusinessSiteExecutiveStaffRelationMapper, BusinessSiteExecutiveStaffRelation> implements IBusinessSiteExecutiveStaffRelationService {

    @Resource
    private MeterStaffMapper meterStaffMapper;

    @Autowired
    private IDeptService deptService;


    @Override
    public boolean saveBatch(Long siteExecutiveId, List<MeterStaff> meterStaffList) {
        //先删除所有的绑定关系
        deleteBySiteExecutiveId(siteExecutiveId);
        //新写入绑定关系
        List<BusinessSiteExecutiveStaffRelation> list = new ArrayList<>();
        meterStaffList.forEach(meterStaff -> {
            BusinessSiteExecutiveStaffRelation siteExecutiveStaffRelation = new BusinessSiteExecutiveStaffRelation();
            siteExecutiveStaffRelation.setSiteExecutiveId(siteExecutiveId);
            siteExecutiveStaffRelation.setStaffId(meterStaff.getId());
            list.add(siteExecutiveStaffRelation);
        });
        return this.saveBatch(list);
    }

    @Override
    public List<MeterStaff> getStaffsByExecutiveInfoId(Long siteExecutiveId) {
        QueryWrapper<BusinessSiteExecutiveStaffRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_id", siteExecutiveId);
        List<BusinessSiteExecutiveStaffRelation> list = this.baseMapper.selectList(queryWrapper);
        List<Dept> deptList = deptService.list();
        Map<Long, String> deptMap = deptList.stream()
                .collect(Collectors.toMap(Dept::getId, Dept::getSimpleName));
        if (list != null && list.size() > 0) {
            List<MeterStaff> meterStaffList = meterStaffMapper.selectBatchIds(list.stream().map(BusinessSiteExecutiveStaffRelation::getStaffId).collect(Collectors.toList()));
            meterStaffList.forEach(
                    meterStaff -> meterStaff.setDeptName(deptMap.get(Long.valueOf(meterStaff.getDeptId())))
                    );
            return meterStaffList;
        }
        return null;
    }


    @Override
    public Boolean deleteBySiteExecutiveId(Long siteExecutiveId) {
        QueryWrapper<BusinessSiteExecutiveStaffRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_id", siteExecutiveId);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }

    @Override
    public Boolean deleteBySiteExecutiveIds(List<Long> siteExecutiveIds) {
        QueryWrapper<BusinessSiteExecutiveStaffRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("site_executive_id", siteExecutiveIds);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }
}
