package com.goafanti.achievement.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.goafanti.achievement.service.AchievementPublishPageService;
import com.goafanti.achievement.service.AchievementPublishService;
import com.goafanti.achievement.service.AchievementService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.dao.AchievementKeywordMapper;
import com.goafanti.common.dao.AchievementPublishMapper;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.enums.AchievementAuditStatus;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.Achievement;
import com.goafanti.common.model.AchievementPublish;
import com.goafanti.common.model.UserIdentity;
import com.goafanti.common.service.BranchInformationService;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.portal.bo.AchievementResultObject;
import com.goafanti.user.service.UserService;

@RestController
@RequestMapping(value = "/api/admin/achievement")
public class AdminAchievementApiController extends CertifyApiController {
    @Resource
    private AchievementService		achievementService;
    @Resource
    private UserService				userService;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    AchievementPublishService	achievementPublishService;
    @Resource
    BranchInformationService	branchInformationService;
	@Autowired
	private AchievementKeywordMapper		achievementKeywordMapper;
	@Autowired
	private AchievementPublishMapper achievementPublishMapper;
    /**
     * 个人成果所有人列表
     */
    @RequestMapping(value = "/userOwner", method = RequestMethod.GET)
    public Result getAchievementUserOwner(String name) {
        Result res = new Result();
        if (StringUtils.isBlank(name)) {
            return res;
        }
        res.setData(userService.selectAchievementUserOwner(name));
        return res;
    }
    /**
     * 组织成果所有人列表
     */
    @RequestMapping(value = "/orgOwner", method = RequestMethod.GET)
    public Result getAchievementOrgOwner(String name) {
        Result res = new Result();
        if (StringUtils.isBlank(name)) {
            return res;
        }
        res.setData(userService.selectAchievementOrgOwner(name));
        return res;
    }
    
    @RequestMapping(value = "/list")
    public Result list(String name,String ownerName,Integer auditStatus,Integer category,Integer dataCategory,
    		String startDate,String endDate,Integer pageNo, Integer pageSize){
    	Result res = new Result();
    	res.setData(achievementService.listAchievement(name, ownerName, category, dataCategory, auditStatus, startDate, endDate, pageNo, pageSize));
    	return res;
    }
    
    @RequestMapping(value="/audit" ,method=RequestMethod.POST)
    private Result auditAchievemnt(String id,Integer auditResult,String auditInfo) {
    	Result result=new Result();
    	if(id == null || auditResult == null || auditResult.intValue()!=0 && auditResult.intValue()!=1)
    	{
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));return result;
    	}
    	AchievementResultObject achievementResultObject=achievementService.getAchievementDetailById(id);
    	if(achievementResultObject==null) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"未找到成果","成果Id"));return result;
    	}
    	if(achievementResultObject.getAuditStatus()!= AchievementAuditStatus.SUBMIT.getCode()) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"未找到成果","当前状态不可审核"));return result;
    	}
    	achievementService.updateAuditAchievement( id,auditResult,auditInfo);
    	return result; 
    }
    
  //撤销成果
  		@RequestMapping(value="/annul", method = RequestMethod.POST)
  		public Result annul(String id){
  			Result res = new Result();
  			if (StringUtils.isBlank(id)) {
  				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","撤销"));
  				return res;
  			}
  			achievementService.updateAuditAchievement( id,0,null);
  			res.setData(1);
  			return res;
  		}
	@RequestMapping(value = "/achievementDetail",method=RequestMethod.GET)
	private Result Detail(String id) {
		Result result=new Result();
		if (StringUtils.isBlank(id)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
			return result;
		}
		
		AchievementResultObject achievementObjects=achievementService.getAchievementDetailById(id);
		List<String>keywords=achievementKeywordMapper.getKeywordsByAchievementId(id);
		List<String> publishPages=achievementPublishMapper.getPublishPagesById(id);
		achievementObjects.setKeywords(keywords);
		achievementObjects.setPublishPages(publishPages);
		result.setData(achievementObjects);
		return result;
	}
    /**
     * 个人成果详情详情
     */
    @RequestMapping(value = "/userDetail", method = RequestMethod.GET)
    private Result userDetail(String id) {
        Result res = new Result();
        if (StringUtils.isBlank(id)) {
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
            return res;
        }
        res.setData(achievementService.selectUserOwnerDetail(id));
        return res;
    }
    /**
     * 组织用户成果详情详情(个人组织合并)
     */
    @RequestMapping(value = "/orgDetail", method = RequestMethod.GET)
    private Result orgDetail(String id,int ownerType) {
        Result res = new Result();
        if (StringUtils.isBlank(id)) {
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
            return res;
        }
        if(ownerType==0){
        	res.setData(achievementService.selectUserOwnerDetail(id));
        }else if(ownerType==1){
        	res.setData(achievementService.selectOrgOwnerDetail(id));
        }       
        return res;
    }
    
   
    /**
     * 成果撤消发布(下架)
     */
    @RequestMapping(value = "/offShelf", method = RequestMethod.POST)
    public Result offShelf(String id,Integer releaseStatus) {
        Result res = new Result();
        if (StringUtils.isBlank(id)) {
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
            return res;
        }
        Achievement a = achievementService.selectByPrimaryKey(id);
        if (null == a) {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
            return res;
        }
        a.setReleaseStatus(releaseStatus);
        res.setData(achievementService.updateReleaseStatus(a));
        return res;
    }
   
   
    /**
     * 图片上传
     */
    @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
    public Result uploadPicture(HttpServletRequest req, String sign) {
        Result res = new Result();
        AttachmentType attachmentType = AttachmentType.getField(sign);
        if (attachmentType == AttachmentType.ACHIEVEMENT_COVER_PICTURE) {
            res.setData(handleFiles(res, "/achievement/", false, req, sign, "achievement"));
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
        }
        return res;
    }
 
 
    /**
     * 成果发布
     */
    @RequestMapping(value = "/addAchievementPublish", method = RequestMethod.POST)
    private Result addAchievementPublish(AchievementPublish a) {
    	Result res = new Result();
    	if (StringUtils.isBlank(a.getAchievementId())) {
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果", "成果"));
            return res;
        }	
        res.setData(achievementPublishService.insertachievementPublish(a));     
        return res;
    }
    /**
     * 撤销发布
     */
    @RequestMapping(value = "/deletePublish", method = RequestMethod.GET)
    private Result deletePublish(String id) {
        Result res = new Result();
        res.setData(achievementPublishService.deletePublish(id));     
        return res;
    }
    /**
     * 修改发布
     */
    @RequestMapping(value = "/updatePublish", method = RequestMethod.GET)
    private Result updatePublish(AchievementPublish a) {
        Result res = new Result();
        res.setData(achievementPublishService.updatePublish(a));     
        return res;
    }
    
    /**
     * 发布列表
     */
    @RequestMapping(value = "/listPublish", method = RequestMethod.GET)
    private Result listPublish(String name,String publishPlatform,Integer publishClient,String publishPage,
    		Integer ifTop, Integer pageNo, Integer pageSize,String ownerName) {
        Result res = new Result();
        if (null==pageNo) {
			pageNo=1;
		}
        if (null==pageSize) {
			pageSize=10;
		}
        res.setData(achievementPublishService.listPublish( name, publishPlatform, publishClient, publishPage,
        		 ifTop,  pageNo,  pageSize,ownerName));     
        return res;
    }
    /**
     * 获取分公司信息
     */
    @RequestMapping(value="/getBranchInformation",method = RequestMethod.GET)
    private Result getBranchInformation(){
    	Result res=new Result();
    	return res.data(branchInformationService.getBranchInformation());
    }
    /**
     * 获取成果页面位置
     */
    @RequestMapping(value="/getPublishPage",method = RequestMethod.GET)
    private Result getPublishPage(){
    	Result res=new Result();
    	return res.data(AchievementPublishPageService.getBranchInformation());
    }
}