package cn.com.countrygarden.bi.main.controller.advise;

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.advise.param.AdviseFeedbackParam;
import cn.com.countrygarden.bi.main.dto.advise.param.AfAdviseFeedbackAddParam;
import cn.com.countrygarden.bi.main.dto.advise.param.ProcessDistributionParam;
import cn.com.countrygarden.bi.main.dto.advise.param.ReplyAfAdviseParam;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseFeedbackService;
import cn.com.countrygarden.bi.main.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 建言-控制层实现
 *
 * @author 木木子
 * @email 944528607@qq.com
 * @url 
 * @date 2019-04-15
 */
@RestController("AfAdviseFeedbackController")
@RequestMapping("/api/afAdviseFeedback")
public class AfAdviseFeedbackController extends BaseController{
    @Autowired
    private IAfAdviseFeedbackService afAdviseFeedbackService;

    @Autowired
    private  FileUploadUtil fileUploadUtil;

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(AdviseFeedbackParam adviseFeedbackParam,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
    	adviseFeedbackParam.setUserCode(getAccessToken().getUserCode());
        return  afAdviseFeedbackService.queryPageList(adviseFeedbackParam,page,size);
    }
   
    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result one(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){

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

        AdviseFeedbackParam adviseFeedbackParam=new AdviseFeedbackParam();
            adviseFeedbackParam.setUserCode(getAccessToken().getUserCode());
        return  afAdviseFeedbackService.editDate(id,adviseFeedbackParam);
    }


    @PostMapping(value = "/addOrEdit")
    public Result addOrEdit(AfAdviseFeedbackAddParam afAdviseFeedbackAddParam,
                            HttpServletRequest request){
    	if(afAdviseFeedbackAddParam==null) {
    		  return Response.response();
    	}

        return  afAdviseFeedbackService.addOrEdit(afAdviseFeedbackAddParam,getAccessToken(request));
    }



    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return  afAdviseFeedbackService.del(id);
    }
    @PostMapping(value = "/getReplyAfAdviseInfo")
    public Result replyAfAdvise(ReplyAfAdviseParam replyAfAdviseParam,
                      HttpServletRequest request){
      if(replyAfAdviseParam==null){
          return Response.response();
      }




        return  afAdviseFeedbackService.getReplyAfAdviseInfo(replyAfAdviseParam,getAccessToken(request));
    }
    
    
    @PostMapping(value = "/addOrEditDistribution")
    public Result addOrEditDistribution(ProcessDistributionParam processDistributionParam,
                                        HttpServletRequest request){
    
        return  afAdviseFeedbackService.addOrEditDistribution(processDistributionParam ,getAccessToken(request));
    }

    /**
     * 获得当前登录用户的所属区域与项目数据
     * @param request
     * @return
     */
    @GetMapping(value = "/getRegionDate")
    public Result getRegionDate(@RequestParam(value = "organCode", required = false)String organCode,
                                        HttpServletRequest request){
        /**
         * 建言-区域管理员(adviseRegionAdmin)
         */
        AdviseFeedbackParam adviseFeedbackParam=new AdviseFeedbackParam();
        boolean regionAdminRole = hasRole("adviseRegionAdmin");
        adviseFeedbackParam.setRegionAdminRole(regionAdminRole);
        adviseFeedbackParam.setUserCode(getAccessToken(request).getUserCode());
        return  afAdviseFeedbackService.getRegionDate(organCode,adviseFeedbackParam);
    }

    @ResponseBody
    @RequestMapping(value = "/fileUpload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result upload(HttpServletRequest request,
                         @RequestParam(value = "fileName", defaultValue = "", required = true) String fileName,
                         @RequestParam(value = "file", defaultValue = "", required = true) MultipartFile multipartFile)
            throws ServiceException {

        if (multipartFile == null) {
            throw new ServiceException("请选择需要上传的文件");
        } else if (fileName == null) {
            throw new ServiceException("请输入文件名");
        }
        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
        if (commonsMultipartFile.isEmpty()) {
            return Response.responseFail("上传文件为空文件");
        }

        return Response.response(fileUploadUtil.fileUpload(multipartFile, request));

    }


}