package com.unis.base.server.domain;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.dao.PersonMeetingDao;
import com.unis.base.server.entity.MeetingRecordDO;
import com.unis.base.server.entity.PersonMeetingDO;
import com.unis.base.server.entity.TrainDO;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.LsBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

@Component
public class PersonMeetingDomain {
    @Autowired
    private PersonMeetingDao personMeetingDao;


    /*public void saveMeeting(PersonMeetingVO personMeetingVO) {
        personMeetingDao.insertOne(personMeetingVO);
    }*/

    public PageInfo<TrainVO> pageMeeting(Integer pageNum, Integer pageSize,String trainName,String status,String trainPersonDeptCode) {
        PageInfo<TrainVO> pageInfoVo = new PageInfo<>();
        PageHelper.startPage(pageNum, pageSize);
        String userId = CurrentUtil.getUserId();
        List<TrainDO> dolist = personMeetingDao.selectInFo(trainName,status,userId);
        List<TrainDO> Polist = personMeetingDao.selectPerson(trainName,status);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy", Locale.CHINA);
        for (TrainDO trainDO : dolist) {
            if (!StringUtils.isEmpty(trainDO.getReleaseDate())) {
                trainDO.setAnnual(String.format(trainDO.getReleaseDate(), formatter));
            }
        }
        for (TrainDO trainDO : dolist) {
            int count = personMeetingDao.countContent(trainDO.getFid(),trainPersonDeptCode);
            trainDO.setPersons(String.valueOf(count));
        }
        List<TrainDO> Stlist = personMeetingDao.selectStatus(trainName,status);
        for (TrainDO trainDO : dolist) {
            boolean flag = false;
            for (TrainDO aDo : Stlist) {
                if (Objects.equals(trainDO.getFid(), aDo.getFid()) && !StringUtils.isEmpty(aDo.getStatus()) && !aDo.getStatus().equals("2")) {
                    flag = true;
                    trainDO.setStatus("0");
                    break;
                }
            }
            if (!flag){
                trainDO.setStatus("2");
            }
        }
        personMeetingDao.deleteAll();
        if (dolist!=null && dolist.size()>0){
            personMeetingDao.insertBatch(dolist);
        }

        List<TrainDO> list = personMeetingDao.selectAllS();
        PageInfo<TrainDO> pageInfoDo = new PageInfo<>(dolist);
        LsBeanUtils.copyAllProperties(pageInfoDo, pageInfoVo);
        return pageInfoVo;
    }

    /*public List<PersonMeetingVO> selectMeeting(String meetingName) {
        List<PersonMeetingVO> list = personMeetingDao.selectBath(meetingName);
        return list;
    }*/

    public List<MeetingRecordVO> meetingRecodrd(PageRecordSVO recordVO) {

        List<MeetingRecordDO> recordVOS = personMeetingDao.selectRecord();
        personMeetingDao.delectRecord();
        personMeetingDao.insertRecord(recordVOS);
        personMeetingDao.delectOne();
        return personMeetingDao.selectallus(recordVO);

    }

    public PageInfo<ResultRecodrdVO> meetingResultRecodrd(PageResultRecodrdVO resultRecodrdVO) {
        String userId = CurrentUtil.getUserId();
        PageHelper.startPage(resultRecodrdVO.getPageNum(), resultRecodrdVO.getPageSize());
        String trainName = resultRecodrdVO.getTrainName();
        List<ResultRecodrdVO> resultRecodrdVOS = personMeetingDao.selectSoRecords(trainName,userId);
      // List<TrainDO> Stlist = personMeetingDao.selectStatus(trainName);
        /*for (TrainDO trainDO : Stlist) {
            for (ResultRecodrdVO aDo : resultRecodrdVOS) {
                if (Objects.equals(trainDO.getFid(), aDo.getFid()) && !StringUtils.isEmpty(trainDO.getStatus()) && !trainDO.getStatus().equals("1")) {
                    aDo.setStatus("1");
                } else {
                    aDo.setStatus("2");
                }
            }
        }*/
        List<ResultRecodrdVO> persons = personMeetingDao.selectTrain();
        for (ResultRecodrdVO recodrdVO : resultRecodrdVOS) {
            for (ResultRecodrdVO person : persons) {
                if (recodrdVO!=null&&Objects.equals(recodrdVO.getTrainName(), person.getTrainName())) {
                    recodrdVO.setPersons(person.getPersons());
                }
            }
        }
        List<ResultRecodrdVO> joinsList = personMeetingDao.selectJoined();
        for (ResultRecodrdVO recodrdVO : resultRecodrdVOS) {
            for (ResultRecodrdVO vo : joinsList) {
                if (recodrdVO!=null&&Objects.equals(recodrdVO.getFid(), vo.getFid())) {
                    recodrdVO.setJoins(vo.getJoins());
                }
            }
        }
        List<ResultRecodrdVO> certorList = personMeetingDao.selectCreator();
        for (ResultRecodrdVO recodrdVO : resultRecodrdVOS) {
            for (ResultRecodrdVO vo : certorList) {
                if (recodrdVO!=null&&Objects.equals(recodrdVO.getFid(), vo.getFid())) {
                    recodrdVO.setCreator(vo.getCreator());
                }
            }
        }
        PageInfo<ResultRecodrdVO> pageInfoVo = new PageInfo<>(resultRecodrdVOS);
        return pageInfoVo;
    }

    public PageInfo<InsertRecordVO> PersonmeetingRecodrd(MeetingRecodrdVO meetingRecodrdVO) {
        String userId = CurrentUtil.getUserId();
        List<InsertRecordVO> personRecordVOS = personMeetingDao.selectPersonRecord(meetingRecodrdVO.getFid(),meetingRecodrdVO.getTrainPersonName(),meetingRecodrdVO.getTrainPersonDeptName(),userId);
        PageInfo<InsertRecordVO> pageInfoVo = new PageInfo<>(personRecordVOS);
        return pageInfoVo;
    }

    public void SavePersonmeetingRecodrd(InRecordVO inRecordVO) {
        List<InsertRecordVO> voList = inRecordVO.getVoList();
        String creator = CurrentUtil.getUserId();
        for (InsertRecordVO insertRecordVO : voList) {
            insertRecordVO.setUserId(insertRecordVO.getUserId());
            insertRecordVO.setCreator(creator);
            personMeetingDao.updatePerson(insertRecordVO);
        }
        for (InsertRecordVO insertRecordVO : voList) {
            if (insertRecordVO.getSaveOrPublish() == 2) {
                personMeetingDao.updateStatusPs(insertRecordVO);
            } else if (insertRecordVO.getSaveOrPublish() == 1) {
                personMeetingDao.setstatuss(insertRecordVO);
            }

        }

    }


    public PageInfo selectCountPerson(CountPersonmeetingVO countPersonmeetingVO) {
        PageHelper.startPage(countPersonmeetingVO.getPageNum(), countPersonmeetingVO.getPageSize());
        List<CountPersonVO> countPersonVOS = personMeetingDao.selectCountPerson(countPersonmeetingVO);
        List<CountPersonVO> applicationList = personMeetingDao.selectApplication();
        for (CountPersonVO countPersonVO : countPersonVOS) {
            for (CountPersonVO personVO : applicationList) {
                if (personVO.getTrainPersonName() != null
                        && Objects.equals(countPersonVO.getTrainPersonName(), personVO.getTrainPersonName())) {
                    countPersonVO.setTrainPersonDeptName(personVO.getTrainPersonDeptName());
                }
            }
        }
        List<CountPersonVO> InList = personMeetingDao.selectIn();
        for (CountPersonVO countPersonVO : countPersonVOS) {
            for (CountPersonVO personVO : InList) {
                if (Objects.equals(countPersonVO.getTrainPersonName(), personVO.getTrainPersonName())) {
                    countPersonVO.setApplication(personVO.getApplication());
                }
            }
        }
        List<CountPersonVO> OutList = personMeetingDao.selectOut();
        for (CountPersonVO countPersonVO : countPersonVOS) {
            for (CountPersonVO personVO : OutList) {
                if (Objects.equals(countPersonVO.getTrainPersonName(), personVO.getTrainPersonName())) {
                    countPersonVO.setTrain(personVO.getTrain());
                }
            }
        }
        List<CountPersonVO> postList = personMeetingDao.selectPost();
        for (CountPersonVO countPersonVO : countPersonVOS) {
            for (CountPersonVO personVO : postList) {
                if (Objects.equals(countPersonVO.getTrainPersonName(), personVO.getTrainPersonName())) {
                    countPersonVO.setPost(personVO.getPost());
                }
            }
        }
        PageInfo<CountPersonVO> pageInfoVo = new PageInfo<>(countPersonVOS);
        return pageInfoVo;
    }

    public List<MeetingRecordVO> meetingDetialRecodrd(TrainDetialVO trainDetialVO) {

        /*String userId = CurrentUtil.getUserId();
        List<String> userNamelist = personMeetingDao.selectUser(userId);
        String userName = userNamelist.get(0);
        List<String> deptIdList = personMeetingDao.selectDapt(userId);
        String deptId = deptIdList.get(0);
        if (userName != null) {*/
            List<MeetingRecordVO> recordList = personMeetingDao.selectDett(trainDetialVO.getMainFid(),trainDetialVO.getTrainPersonDeptCode());
            return recordList;
       /* }

        List<MeetingRecordVO> meetingRecordVOS = personMeetingDao.meetingDetialRecodrd(deptId);
        return meetingRecordVOS;*/
    }

    public void inmeetingDetialRecodrd(SaveRecordVO saveRecordVO) {
        List<RecordVO> recordVOList = saveRecordVO.getRecordVOList();
        for (RecordVO recordVO : recordVOList) {
            recordVO.setMainFid(saveRecordVO.getMainFid());
            recordVO.setTrainPersonDeptCode(saveRecordVO.getTrainPersonDeptCode());
            recordVO.setFeedbackStatus("2");
        }
        String mainFid = saveRecordVO.getMainFid();
        String trainPersonDeptCode = saveRecordVO.getTrainPersonDeptCode();
        // 先删除之前的数据，再插入新的数据
        personMeetingDao.deleteFeedbackByMainFidAndDeptCode(mainFid, trainPersonDeptCode);
        personMeetingDao.deleteContentByMainFidAndDeptCode(mainFid, trainPersonDeptCode);
        personMeetingDao.insertFed(recordVOList);
        personMeetingDao.insertIn(recordVOList);
        for (RecordVO recordVO : recordVOList) {
            if (saveRecordVO.getSaveOrPublish() == 2) {
                personMeetingDao.updateStatus(recordVO);
                personMeetingDao.fbSjForTrainMain(recordVO.getMainFid());
            } else if (saveRecordVO.getSaveOrPublish() == 1) {
                personMeetingDao.updateS(recordVO);
            }

        }

    }

    public  List<MeetingRecordVO> sentPersonmeetingRecodrd(String fid) {
     return   personMeetingDao.selectAllpp(fid);
    }

    public void delPersonmeetingRecodrd(InsertRecordVO insertRecordVO) {
        insertRecordVO.setFeedbackStatus("1");
        insertRecordVO.setCreatorPost(null);
        personMeetingDao.deleteContent(insertRecordVO);
    }

    public List<MainRecordVO>  commeetingDetialRecodrd(String fid) {
      return personMeetingDao.selefe(fid);
    }

    public void deletmeetingDetialRecodrd(DelGetRecordVO getRecordVO) {
        personMeetingDao.deleteContt(getRecordVO);
    }

    public List<TrainMainVo> pageInfoForPx(TrainMainQueryVo viewObj) {
        List<TrainMainVo> list = personMeetingDao.pageInfoForPx(viewObj);
        return list;
    }

    public int countContent(String fid) {
        return personMeetingDao.countContent(fid,null);
    }
}
