package cn.com.countrygarden.bi.main.controller.personnel;
import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.LabelVo;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.personnel.Personnel;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelCampusevaluationService;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.countrygarden.bi.main.model.personnel.PersonnelTag;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelTagMapper;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelTagService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.Collections;

/**
 * 人才标签-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url www.yuhi.com.cn
 * @date 2018-07-10
 */
@RestController("PersonnelTagController")
@RequestMapping("/api/personnel/tag")
public class PersonnelTagController extends BaseController{
    @Autowired
    private PersonnelTagService personnelTagService;
    @Autowired
    private PersonnelService personnelService;
    
    @Autowired
    private PersonnelCampusevaluationService personnelCampusevaluationService;

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(LabelVo label,
                       @RequestParam(value="page",required=false,defaultValue="10") int page,
                       @RequestParam(value="size",required=false,defaultValue="1") int size,
                       HttpServletRequest request){

        return  personnelTagService.queryPageList(label,page,size, getAccessToken(request));

    }

    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return  personnelTagService.queryById(id);
    }

    @PostMapping(value = "/add")
    public Result add(Personnel personnel,
                      @RequestParam(value = "tagsList", required = true)String tagsList){
        return  personnelTagService.add(personnel,tagsList,getAccessToken());
    }
    
    @GetMapping(value = "/tagid",produces="application/json;charset=utf-8")
    public Result tagid(@RequestParam(value="id",required=true)Integer id){
    	
        return personnelTagService.tagid(id);
    }

    @PutMapping(value = "/edit")
    public Result edit(Personnel personnelTag,
                       @RequestParam(value = "tagsList", required = true)String tagsList){
        return  personnelTagService.edit(personnelTag,tagsList,getAccessToken());
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "uid", required = true)String uid){
        return  personnelTagService.del(uid);
    }
    
    @PostMapping(value="/delpiliang")
    public Result delpiliang(
    	
    		@RequestParam(value="userid",required=false)String userid){
        return  personnelTagService.delpiliang(userid);
    }
    
    @GetMapping(value = "/queryadByUid",produces="application/json;charset=utf-8")
    public Result queryadByUid(@RequestParam(value="search",required=true)String search,HttpServletRequest request){
    	
        return personnelTagService.queryadByUid(search,getAccessToken(request));
    }
    
    @GetMapping(value = "/personneltag",produces="application/json;charset=utf-8")
    public Result personneltag(@RequestParam(value="userId",required=false)String userId,@RequestParam(value="identity",required=false)String identity){
    	
        return personnelTagService.personneltag(userId,identity);
    }
    
    @GetMapping(value = "/personneltaguserid",produces="application/json;charset=utf-8")
    public Result personneltaguserid(@RequestParam(value="userId",required=false)String userId){
    	
        return personnelTagService.personneltaguserid(userId);
    }
    

    /**
     * 文件导入
     */
    @ResponseBody
    @RequestMapping(value = "/upload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "name", defaultValue = "1", required = true) String name,
                         @RequestParam(value = "radio", defaultValue = "0", required = true) Integer radio,HttpServletRequest request,
                         @RequestParam(value = "uploadfiles", defaultValue = "", required = true) MultipartFile multipartFile) throws ServiceException {
        if (multipartFile == null) {
            throw new ServiceException("请选择需要上传的文件");
        }else if(name == null){
            throw new ServiceException("请输入文件名");
        }
        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
        if (commonsMultipartFile.isEmpty()) {
            return Response.responseFail("上传文件为空文件");
        }
        String fileName = commonsMultipartFile.getOriginalFilename();
        if (fileName.indexOf(".xlsx") <= 0 || fileName.indexOf(".xls") <= 0) {
            return Response.responseFail("上传文件应为excel文件格式为：xlsx 或 xls");
        }
       
        
        return Response.response( personnelTagService.excelToObject(multipartFile,fileName,radio,getAccessToken().getUserCode(),getAccessToken(request)));

    }

}