package com.jeeplus.modules.projectSpecialistAssess.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.modules.biddingManagement.entity.BiddingManagementInfo;
import com.jeeplus.modules.projectAssess.entity.ProjectAssessInfo;
import com.jeeplus.modules.projectSpecialistAssess.dao.ProjectSpecialistAssessDao;
import com.jeeplus.modules.projectSpecialistAssess.entity.ProjectSpecialistAssess;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportDataDao;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportData;
import com.jeeplus.modules.projectcontentinfo.service.ProjectReportDataService;
import com.jeeplus.modules.projectrecord.service.ProjectRecordsService;
import com.jeeplus.modules.projectreportnum.service.ProjectReportNumService;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 专家评估报告service
 * @author 徐滕
 * @version 2020-05-15
 */
@Service
@Transactional(readOnly = true)
public class ProjectSpecialistAssessService extends CrudService<ProjectSpecialistAssessDao, ProjectSpecialistAssess> {

    @Autowired
    private ProjectRecordsService projectRecordsService;
    @Autowired
    private ProjectReportDataService projectReportDataService;
    @Autowired
    private ProjectReportNumService projectReportNumService;
    @Autowired
    private WorkattachmentDao workattachmentDao;

    public ProjectSpecialistAssess get(String id) {
        return super.get(id);
    }

    public Page<ProjectSpecialistAssess> findPage(Page<ProjectSpecialistAssess> page, ProjectSpecialistAssess projectSpecialistAssess) {
        //设置数据权限
        if(!UserUtils.getUser().isAdmin()) {
            String dataScopeSql = dataScopeFilterOR(projectSpecialistAssess.getCurrentUser(), "o", "u", "s", MenuStatusEnum.PROJECT_REPORTDATA.getValue());
            projectSpecialistAssess.getSqlMap().put("dsf", dataScopeSql);
        }
        int count = dao.queryCount(projectSpecialistAssess);
        page.setCount(count);
        page.setCountFlag(false);
        projectSpecialistAssess.setPage(page);
        List<ProjectSpecialistAssess> projectAssessList = findList(projectSpecialistAssess);
        page.setList(projectAssessList);
        return page;
    }

    @Transactional(readOnly = false)
    public void save(ProjectSpecialistAssess projectSpecialistAssess) {
        User createBy = projectSpecialistAssess.getCreateBy();
        if(createBy != null){
            createBy = UserUtils.get(createBy.getId());
        }else {
            createBy = UserUtils.getUser();
        }
        projectSpecialistAssess.setCreateBy(createBy);
        projectSpecialistAssess.setCreateDate(new Date());
        projectSpecialistAssess.setUpdateBy(createBy);
        projectSpecialistAssess.setUpdateDate(new Date());
        if(StringUtils.isBlank(projectSpecialistAssess.getId())){
            projectSpecialistAssess.setId(IdGen.uuid());
            dao.insert(projectSpecialistAssess);
        }else {
            super.save(projectSpecialistAssess);
        }
        if (1 == projectSpecialistAssess.getStatus()){
            ProjectReportData projectReportData = projectReportDataService.get(projectSpecialistAssess.getReportId());
            String oldAttachmentId = projectReportData.getId();
            String newAttachmentId = projectSpecialistAssess.getId();
            projectReportData.setId(newAttachmentId);
            projectReportData.setFileStatus("1");
            projectReportData.setStatus("1");
            String num = projectReportData.getNumber();
            if(com.jeeplus.common.utils.StringUtils.isNotBlank(num)){
                projectReportNumService.updateNum(num);
            }
            //保存新的报告信息
            projectReportDataService.insert(projectReportData);
            //删除老的报告信息
            projectReportData.setId(oldAttachmentId);
            projectReportDataService.delete(projectReportData);
            //修改报告附件对应报告信息的id信息
            workattachmentDao.updateAttachmentId(oldAttachmentId,newAttachmentId);
            projectSpecialistAssess.setReportId(newAttachmentId);
            dao.updateReportId(projectSpecialistAssess);
        }
    }

    @Transactional(readOnly = false)
    public void update(ProjectSpecialistAssess projectSpecialistAssess) {
        User createBy = projectSpecialistAssess.getCreateBy();
        if(createBy != null){
            createBy = UserUtils.get(createBy.getId());
        }else {
            createBy = UserUtils.getUser();
        }
        projectSpecialistAssess.setUpdateBy(createBy);
        projectSpecialistAssess.setUpdateDate(new Date());
        if(StringUtils.isBlank(projectSpecialistAssess.getId())){
            projectSpecialistAssess.setId(IdGen.uuid());
            dao.insert(projectSpecialistAssess);
        }else {
            dao.update(projectSpecialistAssess);
        }
    }

    @Transactional(readOnly = false)
    public void delete(ProjectSpecialistAssess projectSpecialistAssess) {
        dao.delete(projectSpecialistAssess);
    }
}
