package com.goafanti.achievement.controller;

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

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.AchievementOrderService;
import com.goafanti.achievement.service.AchievementPublishPageService;
import com.goafanti.achievement.service.AchievementPublishService;
import com.goafanti.achievement.service.AchievementService;
import com.goafanti.admin.service.AftFileService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.Achievement;
import com.goafanti.common.model.AchievementPublish;
import com.goafanti.common.model.AftFile;
import com.goafanti.common.service.BranchInformationService;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;
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 AftFileService			aftFileService;
    @Resource
    private AchievementOrderService	achievementOrderService;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    AchievementPublishService	achievementPublishService;
    @Resource
    BranchInformationService	branchInformationService;
    
    /**
     * 个人成果列表
     */
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    private Result userList(Integer ownerType,String username, String ownerName, String ownerId, Integer auditStatus,
            Integer serialNumber, String name, String keyword, Integer category, Integer status,
            String releaseDateStartDate, String releaseDateEndDate,String createDateStartDate, String createDateEndDate, Integer releaseStatus, String pageNo,
            String pageSize,Integer boutique,Integer hot) {
        Result res = new Result();
        Integer pNo = 1;
        Integer pSize = 10;
        if (StringUtils.isNumeric(pageSize)) {
            pSize = Integer.parseInt(pageSize);
        }
        if (StringUtils.isNumeric(pageNo)) {
            pNo = Integer.parseInt(pageNo);
        }
       res.setData(achievementService.listUserOwnerAchievement(ownerType,username, ownerName, ownerId, auditStatus, serialNumber, name,
                keyword, category, status, releaseDateStartDate, releaseDateEndDate,createDateStartDate,createDateEndDate, releaseStatus, pNo, pSize,boutique,hot));
       return res;
    }
    /**
     * 组织用户成果列表(个人组织合并)
     */
    @RequestMapping(value = "/orgList", method = RequestMethod.GET)
    private Result orgList(Integer ownerType,String unitName, String ownerName, String ownerId, Integer auditStatus, Integer serialNumber, String name,
            String keyword, Integer category, Integer status, String releaseDateStartDate, String releaseDateEndDate,String createDateStartDate, String createDateEndDate,
            Integer releaseStatus,String internationalFlag, String pageNo, String pageSize,Integer boutique,Integer hot,String recordPerson) {
        Result res = new Result();
        Integer pNo = 1;
        Integer pSize = 10;
        if (StringUtils.isNumeric(pageSize)) {
            pSize = Integer.parseInt(pageSize);
        }
        if (StringUtils.isNumeric(pageNo)) {
            pNo = Integer.parseInt(pageNo);
        }
        
        Object o = achievementService.listOrgOwnerAchievement(ownerType,unitName, ownerName, ownerId, auditStatus, serialNumber, name,
                keyword, category, status, releaseDateStartDate, releaseDateEndDate, createDateStartDate,  createDateEndDate, releaseStatus,internationalFlag, pNo, pSize,boutique,hot, recordPerson);
        
        res.setData(o);
        
        
        return res;
    }
    /**
     * 个人成果所有人列表
     */
    @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 = "/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;
    }
    
    /**
     * 下载技术成果批量导入Excel模板
     * 
     * @param response
     * @return
     */
    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public Result downloadTemplateFile(HttpServletResponse response, String sign) {
        Result res = new Result();
        AttachmentType attachmentType = AttachmentType.getField(sign);
        if (attachmentType == AttachmentType.ACHIEVEMENT_TEMPLATE) {
            String fileName = "";
            AftFile af = aftFileService.selectAftFileBySign(sign);
            if (null == af) {
                res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件！"));
            } else {
                String path = af.getFilePath();
                String suffix = path.substring(path.lastIndexOf("."));
                fileName = AttachmentType.ACHIEVEMENT_TEMPLATE.getDesc() + suffix;
                downloadFile(response, fileName, path);
            }
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
        }
        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 = "/download", method = RequestMethod.GET)
    public Result download(HttpServletResponse response, String id, String sign) {
        Result res = new Result();
        if (StringUtils.isEmpty(id)) {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技成果ID"));
            return res;
        }
        Achievement a = achievementService.selectByPrimaryKey(id);
        if (null == a) {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技成果ID"));
            return res;
        }
        AttachmentType attachmentType = AttachmentType.getField(sign);
        if (attachmentType == AttachmentType.ACHIEVEMENT_MATURITY_TEXT_FILE) {
            downloadUnPrivateFile(response, a.getMaturityTextFileDownloadFileName(), a.getMaturityTextFileUrl());
        } else if (attachmentType == AttachmentType.ACHIEVEMENT_TECH_PLAN) {
            downloadUnPrivateFile(response, a.getTechPlanDownloadFileName(), a.getTechPlanUrl());
        } else if (attachmentType == AttachmentType.ACHIEVEMENT_BUSINESS_PLAN) {
            downloadUnPrivateFile(response, a.getBusinessPlanDownloadFileName(), a.getBusinessPlanUrl());
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
        }
        return res;
    }
    /**
     * 文本文件上传
     */
    @RequestMapping(value = "/uploadTextFile", method = RequestMethod.POST)
    public Result uploadTextFile(HttpServletRequest req, String sign) {
        Result res = new Result();
        AttachmentType attachmentType = AttachmentType.getField(sign);
        if (attachmentType == AttachmentType.ACHIEVEMENT_MATURITY_TEXT_FILE
                || attachmentType == AttachmentType.ACHIEVEMENT_TECH_PLAN
                || attachmentType == AttachmentType.ACHIEVEMENT_BUSINESS_PLAN) {
            res.setData(handleFiles(res, "/achievement/", false, req, sign, "achievement"));
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
        }
        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_TECHNICAL_PICTURE
                || attachmentType == AttachmentType.ACHIEVEMENT_MATURITY_PICTURE
                || 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 = "/myList", method = RequestMethod.GET)
    private Result myList(Integer ownerType,String unitName, String ownerName, String ownerId, Integer auditStatus, Integer serialNumber, String name,
            String keyword, Integer category, Integer status, String releaseDateStartDate, String releaseDateEndDate,String createDateStartDate, String createDateEndDate,
            Integer releaseStatus,String internationalFlag, String pageNo, String pageSize,Integer boutique,Integer hot) {
        Result res = new Result();
        Integer pNo = 1;
        Integer pSize = 10;
        if (StringUtils.isNumeric(pageSize)) {
            pSize = Integer.parseInt(pageSize);
        }
        if (StringUtils.isNumeric(pageNo)) {
            pNo = Integer.parseInt(pageNo);
        }
        String techBrokerId=TokenManager.getAdminId();
        Object o = achievementService.listMyAchievement(techBrokerId,ownerType,unitName, ownerName, ownerId, auditStatus, serialNumber, name,
                keyword, category, status, releaseDateStartDate, releaseDateEndDate, createDateStartDate,  createDateEndDate, releaseStatus,internationalFlag, pNo, pSize,boutique,hot);
        res.setData(o);     
        return res;
    }
    
    /**
     * 科技成果管理列表
     */
    @RequestMapping(value = "/manageList", method = RequestMethod.GET)
    private Result manageList(Integer ownerType,String unitName, String ownerName, String ownerId, Integer auditStatus, Integer serialNumber, String name,
            String keyword, Integer category, Integer status, String releaseDateStartDate, String releaseDateEndDate,String createDateStartDate, String createDateEndDate,
            Integer releaseStatus,String internationalFlag, String pageNo, String pageSize,Integer boutique,Integer hot,String recordPerson) {
        Result res = new Result();
        Integer pNo = 1;
        Integer pSize = 10;
        if (StringUtils.isNumeric(pageSize)) {
            pSize = Integer.parseInt(pageSize);
        }
        if (StringUtils.isNumeric(pageNo)) {
            pNo = Integer.parseInt(pageNo);
        }
        String techBrokerId=TokenManager.getAdminId();
        Object o = achievementService.listManageAchievement(techBrokerId,ownerType,unitName, ownerName, ownerId, auditStatus, serialNumber, name,
                keyword, category, status, releaseDateStartDate, releaseDateEndDate, createDateStartDate,  createDateEndDate, releaseStatus,internationalFlag, pNo, pSize,boutique,hot,recordPerson);
        res.setData(o);     
        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());
    }
}