package cn.com.countrygarden.bi.main.controller.ck.satisfaction;


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.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.ck.satisfaction.SatisfiedNationwide;
import cn.com.countrygarden.bi.main.service.ck.SatisfiedNationwideService;
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;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *   满意度-全国模块接口
 * </p>
 *
 * @author
 * @date 2021-08-06
 * @version V1.0
 */

@RestController("SatisfiedNationwideController")
@RequestMapping("/api/satisfiedNationwide")
public class SatisfiedNationwideController extends BaseController {
    @Autowired
    private SatisfiedNationwideService satisfiedNationwideService;

    @GetMapping(value = "/getRegionDate", produces = "application/json;charset=utf-8")
    public Result getRegionDate(HttpServletRequest request) {

        return Response.response(satisfiedNationwideService.getRegionDate(getAccessToken(request)));
    }

    @GetMapping(value = "/getPojDate", produces = "application/json;charset=utf-8")
    public Result getPojDate(HttpServletRequest request,
                             @RequestParam(value = "organCode",required = true) String organCode) {

        return Response.response(satisfiedNationwideService.getPojDate(getAccessToken(request),organCode));
    }

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(SatisfiedNationwide satisfiedNationwide,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request) {
        return satisfiedNationwideService.queryPageList(satisfiedNationwide,page,size,getAccessToken(request));
    }

    @PutMapping(value = "/edit")
    public Result edit(SatisfiedNationwide satisfiedNationwide, HttpServletRequest request){
        return  satisfiedNationwideService.edit(satisfiedNationwide,getAccessToken(request));
    }

    @PostMapping(value = "/getrevenue")
    public Result getrevenue(HttpServletRequest request) throws IOException {
        return satisfiedNationwideService.getrevenue(getAccessToken(request),request);
    }

    @PutMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)String id){
        return  satisfiedNationwideService.del(id,getAccessToken());
    }

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


        if (multipartFile == null) {
            throw new ServiceException("请选择需要上传的文件");
        }
        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
        if (commonsMultipartFile.isEmpty()) {
            return Response.responseFail("上传文件为空文件");
        }
        String fileName = commonsMultipartFile.getOriginalFilename();
        if (fileName.indexOf(".xlsx") != -1) {
            return satisfiedNationwideService.excelToxls(request,response,multipartFile, fileName,usname,
                    getAccessToken().getUserCode(), getAccessToken(request));
        }
        return Response.responseFail("模板有误，请下载对应的模版");
    }

}