/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.projectcontentinfo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectBasedDataDao;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectcontentinfoDao;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectBasedData;
import com.jeeplus.modules.projectcontentinfo.entity.Projectcontentinfo;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.workreview.entity.WorkReviewAudit;
import com.jeeplus.modules.workreview.entity.WorkReviewStandard;
import com.jeeplus.modules.workreview.service.WorkReviewStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectContentData;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectContentDataDao;

/**
 * 工作内容详情Service
 * @author yangfan
 * @version 2018-06-05
 */
@Service
@Transactional(readOnly = true)
public class ProjectContentDataService extends CrudService<ProjectContentDataDao, ProjectContentData> {

    @Autowired
    private WorkattachmentDao workattachmentDao;
    @Autowired
    private ProjectBasedDataDao projectBasedDataDao;
    @Autowired
    private ProjectcontentinfoDao projectcontentinfoDao;
    @Autowired
    private WorkReviewStandardService workReviewStandardService;

	public ProjectContentData get(String id) {
        ProjectContentData projectContentData = super.get(id);
        if(projectContentData!=null){
            Workattachment workattachment = new Workattachment();
            workattachment.setAttachmentId(id);
            projectContentData.setWorkAttachments(workattachmentDao.findList(workattachment));
        }
        return projectContentData;
    }

    public void queryBasedData(ProjectContentData projectContentData){
	    if (projectContentData==null || StringUtils.isBlank(projectContentData.getId()))return;
        List<ProjectBasedData> list = projectBasedDataDao.queryListByContentId(projectContentData.getId());
        projectContentData.setProjectBasedDataList(list);
    }
	
	public List<ProjectContentData> findList(ProjectContentData projectContentData) {
		return super.findList(projectContentData);
	}
	
	public Page<ProjectContentData> findPage(Page<ProjectContentData> page, ProjectContentData projectContentData) {
		return super.findPage(page, projectContentData);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectContentData projectContentData) {
	    if(StringUtils.isBlank(projectContentData.getNumber())){
	        Integer serialNum = dao.querySerialNum(projectContentData);
            if (serialNum==null){
                serialNum=0;
            }
            serialNum+=1;
            String num = String.valueOf(serialNum);
            StringBuffer sb = new StringBuffer();
            for(int x = 0; x < (3 - num.length()); x++) {
                sb.append("0");
            }
            sb.append(num);
            projectContentData.setNumber(sb.toString());
        }
		super.save(projectContentData);
	}
	
	@Transactional(readOnly = false)
	public void delete(ProjectContentData projectContentData) {
	    //删除附件
	    if(projectContentData.getWorkAttachments()!=null&&!projectContentData.getWorkAttachments().isEmpty()){
            for (Workattachment workattachment : projectContentData.getWorkAttachments()) {
                workattachmentDao.delete(workattachment);
            }
        }
        //删除资料关联
        dao.deleteBasedByContentId(projectContentData.getId());
		super.delete(projectContentData);
	}

    @Transactional(readOnly = false)
    public void saveBasedData(String contentId,List<ProjectBasedData> projectBasedDataList) {
        Iterator<ProjectBasedData> iterator = projectBasedDataList.iterator();
        while (iterator.hasNext()){
            ProjectBasedData basedData = iterator.next();
            if(basedData==null||StringUtils.isBlank(basedData.getId())){
                iterator.remove();
            }
        }
        HashMap<String, Object> map = new HashMap<>(5);
        map.put("contentId", contentId);
        map.put("projectBasedDataList", projectBasedDataList);
        dao.batchInsertBaseData(map);
    }

    @Transactional(readOnly = false)
    public void deleteBased(String contentId,String basedId) {
        int i = dao.deleteBasedData(contentId, basedId);
        if(i==0){
            return;
        }
        //删除菜单
        Projectcontentinfo parent = projectcontentinfoDao.getByInfoId(contentId);
        Projectcontentinfo select2 = new Projectcontentinfo();
        select2.setParent(parent);
        select2.setProject(parent.getProject());
        select2.setName("依据资料");
        List<Projectcontentinfo> infos = projectcontentinfoDao.findListByParentSelective(select2);
        Projectcontentinfo projectcontentinfo = infos.get(0);
        select2.setParent(projectcontentinfo);
        select2.setName(null);
//        select2.setLinkId(basedId);
        select2.setInfoId(basedId);
        //删除当前菜单
        projectcontentinfoDao.deleteByParentAndLinkId(select2);
        //是否删除父级菜单
        select2.setInfoId(null);
        select2.setLinkId(null);
        List<Projectcontentinfo> sonList = projectcontentinfoDao.findListByParentSelective(select2);
        if(sonList==null || sonList.isEmpty()){
            projectcontentinfoDao.delete(projectcontentinfo);
        }
    }

    @Transactional(readOnly = false)
    public void deleteContent(String contentId,String reportId,String projectId,String type) {
        //String linkId = dao.findByLinkId(contentId,projectId);
        dao.deleteReportDataInfo(contentId,reportId,type);
        dao.deleteReportDataMenu(contentId,projectId,reportId);
    }
    @Transactional(readOnly = false)
    public void saveReportContent(String contentId,String reportId,String type) {
        dao.saveReportContent(contentId,reportId,type);
    }

    public Integer countBased(String basedId) {
        return dao.countByBaseId(basedId);
    }
    public List<WorkReviewAudit> findListReviewAudit(String reviewId,String name,String infoId) {
        WorkReviewStandard workReviewStandard = new WorkReviewStandard();
        workReviewStandard.setStandardDetail(name);
        String reviewIds = "0,"+reviewId+",";
        workReviewStandard.setParentIds(reviewIds);
        workReviewStandard.setType(reviewId);
        List<WorkReviewStandard> workReviewStandards = workReviewStandardService.findListByType(workReviewStandard);
        List<WorkReviewAudit> workReviewAudits = new ArrayList<>();
        for (WorkReviewStandard reviewStandard:workReviewStandards){
            int orderNum = Integer.parseInt(reviewStandard.getOrderNum());
            if (orderNum>999){
                WorkReviewAudit workReviewAudit = new WorkReviewAudit();
                workReviewAudit.setReportId(infoId);
                workReviewAudit.setMaxDeduct(reviewStandard.getMaxDeduct());
                workReviewAudit.setOrderNum(reviewStandard.getOrderNum());
                workReviewAudit.setStandardDetail(reviewStandard.getStandardDetail());
                workReviewAudit.setSort(reviewStandard.getSort());
                workReviewAudit.setId(reviewStandard.getId());
                if (orderNum>999 && orderNum<2000){
					workReviewAudit.setType("1");
					workReviewAudit.setParent(new WorkReviewAudit("0"));
					workReviewAudit.setParentIds("0,");
                }
                if (StringUtils.isNotBlank(workReviewAudit.getType())) {
                    workReviewAudits.add(workReviewAudit);
                }
            }
        }
        return workReviewAudits;
    }
}