package com.zcloud.service.safetyMeeting.impl;

import com.alibaba.fastjson.JSON;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.safetyMeeting.SafetyMeetingMapper;
import com.zcloud.mapper.datasource.safetyMeeting.SafetyMeetingPeopleMapper;
import com.zcloud.service.safetyMeeting.SafetyMeetingService;
import com.zcloud.util.InitPageDataUtil;
import com.zcloud.util.Jurisdiction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 说明：TODO
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional
public class SafetyMeetingServiceImpl implements SafetyMeetingService {

    @Resource
    private SafetyMeetingMapper safetyMeetingMapper;
    @Resource
    private SafetyMeetingPeopleMapper safetyMeetingPeopleMapper;
    @Resource
    private InitPageDataUtil initPageDataUtil;

    @Override
    public void save(PageData pageData) {
        List<PageData> dataList = JSON.parseArray(pageData.getString("people"), PageData.class);
        List<PageData> safetyMeetingPeopledataList = new ArrayList<>();
        AtomicInteger peopleCount = new AtomicInteger();
        dataList.forEach(item ->{
            String DEPARTMENT_ID = item.getString("DEPARTMENT_ID");
            String POST_ID = item.getString("POST_ID");
            List<String> userIds = JSON.parseArray(item.getString("USER_ID"),String.class);
            peopleCount.addAndGet(userIds.size());
            for (String userId : userIds) {
                PageData safetyMeetingPeopledata = new PageData();
                safetyMeetingPeopledata.put("SAFETY_MEETING_ID",pageData.getString("SAFETY_MEETING_ID"));
                safetyMeetingPeopledata.put("USER_ID",userId);
                safetyMeetingPeopledata.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
                safetyMeetingPeopledata.put("POST_ID", POST_ID);
                safetyMeetingPeopledata.put("DEPARTMENT_ID", DEPARTMENT_ID);
                initPageDataUtil.initSave(safetyMeetingPeopledata,"SAFETY_MEETING_PEOPLE_ID");
                safetyMeetingPeopledataList.add(safetyMeetingPeopledata);
            }
        });
        pageData.put("ALL_SIGNATURES",peopleCount.get());
        pageData.put("SIGNATURES",0);
        safetyMeetingMapper.save(pageData);
        safetyMeetingPeopleMapper.saveBatch(safetyMeetingPeopledataList);
    }

    @Override
    public void removeById(PageData pageData) {
        safetyMeetingMapper.removeById(pageData);
    }

    @Override
    public List<PageData> list(Page page) {
        return safetyMeetingMapper.datalistPage(page);
    }

    @Override
    public PageData findById(PageData pageData) {
        return safetyMeetingMapper.findById(pageData);
    }

    @Override
    public void edit(PageData pageData) {
        //  首先把 涉及人删掉
        safetyMeetingPeopleMapper.removeBySafetMeetingId(pageData);
        initPageDataUtil.initEdit(pageData);
        List<PageData> dataList = JSON.parseArray(pageData.getString("people"), PageData.class);
        List<PageData> safetyMeetingPeopledataList = new ArrayList<>();
        AtomicInteger peopleCount = new AtomicInteger();
        dataList.forEach(item ->{
            String DEPARTMENT_ID = item.getString("DEPARTMENT_ID");
            String POST_ID = item.getString("POST_ID");
            List<String> userIds = JSON.parseArray(item.getString("USER_ID"),String.class);
            peopleCount.addAndGet(userIds.size());
            for (String userId : userIds) {
                PageData safetyMeetingPeopledata = new PageData();
                safetyMeetingPeopledata.put("SAFETY_MEETING_ID", pageData.getString("SAFETY_MEETING_ID"));
                safetyMeetingPeopledata.put("USER_ID", userId);
                safetyMeetingPeopledata.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
                safetyMeetingPeopledata.put("POST_ID", POST_ID);
                safetyMeetingPeopledata.put("DEPARTMENT_ID", DEPARTMENT_ID);
                initPageDataUtil.initSave(safetyMeetingPeopledata, "SAFETY_MEETING_PEOPLE_ID");
                safetyMeetingPeopledataList.add(safetyMeetingPeopledata);
            }
        });
        safetyMeetingMapper.edit(pageData);
        // 把删除的人重新激活
        safetyMeetingPeopleMapper.saveOrUpdateBatch(safetyMeetingPeopledataList);
        // 清理掉被删除人的反馈
        safetyMeetingPeopleMapper.removeBySafetMeetingUserFeedback(pageData);

    }

    @Override
    public List<PageData> saftPersonList(PageData pageData) {
        return safetyMeetingMapper.saftPersonList(pageData);
    }

    @Override
    public List<PageData> appDataList(Page page) {
        return safetyMeetingMapper.appdatalistPage(page);
    }

    @Override
    public void confirmMeetingPeople(PageData pageData) {
        safetyMeetingMapper.confirmMeetingPeople(pageData);
    }

    @Override
    public PageData findMeetPeopleByUId(PageData pageData) {
        return safetyMeetingMapper.findMeetPeopleByUId(pageData);
    }

    @Override
    public List<PageData> getfeedbackList(PageData pageData) {
        return safetyMeetingMapper.getfeedbackList(pageData);
    }
}
