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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.JudgeDao;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Judge;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workfullmanage.service.WorkFullManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 评委Service
 * @author fgy
 * @version 2017-12-26
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true)
public class JudgeService extends CrudService<JudgeDao, Judge> {

	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
    private WorkattachmentDao workattachmentDao;
	@Autowired
    private JudgeDao judgeDao;
	@Autowired
    private WorkFullManageService workFullManageService;
	@Autowired
    private SystemService systemService;
	@Autowired
    private UserDao userDao;

	public Judge get(String id) {
		Judge judge = super.get(id);
		if(judge==null)return new Judge();
		List<Workattachment> attachmentList = workattachmentService.getListByAttachmentIdAndFlag(judge.getId(),"65");
		if(attachmentList!=null && attachmentList.size()>0){
            for (int i = 0; i < attachmentList.size(); i++) {
                Workattachment w = attachmentList.get(i);
                if(w!=null){
                    w.setAttachmentUser(UserUtils.get(w.getAttachmentUser()).getName());
                }
            }
        }
		judge.setJudgeAttachmentList(attachmentList);
		judge.setWorkAttachments(attachmentList);
		return judge;
	}
	
	public List<Judge> findList(Judge judge) {
		return super.findList(judge);
	}
	
	public Page<Judge> findPage(Page<Judge> page, Judge judge) {
		return super.findPage(page, judge);
	}
	
	@Transactional(readOnly = false)
	public void save(Judge judge,String attachmentId) {
	    String id = judge.getId();
	    try {
            super.save(judge);
//            if(StringUtils.isNotBlank(attachmentId)){
//                workattachmentDao.updateAttachmentId(attachmentId,judge.getId());
//            }
            if(StringUtils.isBlank(id)){
                List<Workattachment> workAttachments = judge.getWorkAttachments();
                if(workAttachments!=null && workAttachments.size()>0){
                    for (int i = 0; i < workAttachments.size(); i++) {
                        Workattachment w = workAttachments.get(i);
                        if (StringUtils.isNotBlank(w.getUrl()) && !w.getUrl().equals("null")) {
                            w.setCompanyId(judge.getCompanyId());
                            w.setAttachmentUser(UserUtils.getUser().getId());
                            w.setAttachmentId(judge.getId());
                            w.setId("");
                            workattachmentService.save(w);
                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
	}
	
	@Transactional(readOnly = false)
	public void delete(Judge judge) {
		super.delete(judge);
	}


	@Transactional(readOnly = false)
    public void delFile(String attaId, String attachmentUrl) {
		try {
			Workattachment workattachment = new Workattachment();
			if(StringUtils.isNotBlank(attaId)){
				workattachment.setId(attaId);
				workattachmentService.delete(workattachment);
			}
			if(StringUtils.isNotBlank(attachmentUrl)){
				workattachment.setUrl(attachmentUrl);
				workattachmentService.deleteFileFromAliyun(workattachment,workattachment==null?attachmentUrl:workattachment.getUrl());
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}

    @Transactional(readOnly = false)
    public void saveFile(MultipartHttpServletRequest multiRequest) {
        try {
            String attachmentId = multiRequest.getParameter("attachmentId");
            String currentTime = multiRequest.getParameter("currentTime");
            String time = String.valueOf(multiRequest.getSession().getAttribute("judge_attachment_currentTime"));
            if(StringUtils.isBlank(attachmentId)&&(StringUtils.isBlank(time) || "null".equals(time) || !time.equals(currentTime))){
                time = multiRequest.getParameter("currentTime");
                multiRequest.getSession().setAttribute("judge_attachment_currentTime",time);
            }
            OSSClientUtil ossUtil = new OSSClientUtil();
            Iterator<String> fileNames = multiRequest.getFileNames();
            while(fileNames.hasNext()){
                MultipartFile file = multiRequest.getFile(fileNames.next());
                String fileName = file.getOriginalFilename();
                String fileType = fileName.substring(fileName.lastIndexOf("."));
                if(file != null && !file.isEmpty()){
                    String aliyunPath = ossUtil.uploadFile2OSS(file, "judgeAttachment");
                    Workattachment workattachment = new Workattachment();
                    workattachment.setUrl(aliyunPath);
                    workattachment.setType(fileType);
                    workattachment.setAttachmentId(StringUtils.isNotBlank(attachmentId)?attachmentId:time);
                    workattachment.setAttachmentUser(UserUtils.getUser().getId());
                    workattachment.setAttachmentName(fileName);
                    workattachment.setCompanyId("");   //暂时先不区分评委所属的公司；
                    workattachment.setAttachmentFlag("65");
                    workattachmentService.save(workattachment);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public List<Judge> selectJudgeMembers(String judge, String consultType,String judgeCount) {
	    List<Judge> judgeList = Lists.newArrayList();
        try {
            if(StringUtils.isNotBlank(judge) && StringUtils.isNotBlank(consultType) ){
                Map<String,Object> map = Maps.newHashMap();
                map.put("judge",judge);
                map.put("consultType",consultType);
                map.put("judgeCount",Integer.parseInt(judgeCount));
                judgeList = judgeDao.selectJudgeMembers(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return judgeList;
    }
    public List<Judge> selectJudgeMembersBak(String judge, String consultType, String judgeCount,String judgeMembers) {
        List<Judge> judgeBakList = Lists.newArrayList();
        try {
            if(StringUtils.isNotBlank(judge) && StringUtils.isNotBlank(consultType)){
                Map<String,Object> map = Maps.newHashMap();
                map.put("judge",judge);
                map.put("consultType",consultType);
                map.put("judgeCount",Integer.parseInt(judgeCount));
                String[] split = judgeMembers.split(",");
                if(split!=null && split.length>0){
                    map.put("judgeMembers",judgeMembers);
                }
                judgeBakList = judgeDao.selectJudgeMembersBak(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return judgeBakList;
    }

    public String getPeopleMaster() {
        String[] split = UserUtils.getUser().getId().split(",");
        String peopleMaster = workFullManageService.getPeopleMaster(split);
        return peopleMaster;
    }
}