package com.gree.mall.manager.controller.common;

import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.ExpressLogic;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.common.kuaidi100.response.SubscribeResp;
import com.gree.mall.manager.plus.entity.CommonFile;
import com.gree.mall.manager.plus.entity.ExpressCompany;
import com.gree.mall.manager.plus.entity.ExpressInfo;
import com.gree.mall.manager.plus.entity.Region;
import com.gree.mall.manager.plus.service.OrderDetailService;
import com.gree.mall.manager.plus.service.OrderInfoService;
import com.gree.mall.manager.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;


@Slf4j
@RestController
@Api(value = "公共API", tags ={"公共API"} )
@RequestMapping(value = "/common", produces = "application/json; charset=utf-8")
public class CommonController {

    @Autowired
    CommonLogic commonLogic;
    @Autowired
    ExpressLogic expressLogic;
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    OrderDetailService orderDetailService;



    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public ResponseHelper<CommonFile> upload(
            @ApiParam(required = true, value = "附件") @RequestParam(required = true) MultipartFile file
    ) throws IOException, RemoteServiceException {
        CommonFile commonFile = commonLogic.uploadFile(file);
        return ResponseHelper.success(commonFile);
    }

    @GetMapping("template/download")
    @ApiOperation("下载模板")
    public void download(@RequestParam(required = false) @NotBlank(message = "需下载的模板名称不能为空") String name, HttpServletResponse response) throws IOException {
        CommonUtils.downloadFile(name, response);
    }

    @GetMapping("/express")
    @ApiOperation(value = "查看物流")
    public ResponseHelper<List<ExpressInfo>> express(
            @ApiParam(required = true, value = "物流单号") @RequestParam(required = true) String logisticsNo,
            @ApiParam(required = false, value = "快递公司code") @RequestParam(required = false) String companyCode
    ) throws IOException, RemoteServiceException {
        List<ExpressInfo> expressInfos = expressLogic.queryExpress(logisticsNo,companyCode);
        return ResponseHelper.success(expressInfos);
    }

    @ApiNotAuth
    @PostMapping("/express/callback")
    @ApiOperation(value = "物流回调")
    public SubscribeResp expressCallback(HttpServletRequest request) throws Exception {
        expressLogic.expressCallBack(request);
        SubscribeResp subscribeResp = new SubscribeResp();
        subscribeResp.setResult(Boolean.TRUE);
        subscribeResp.setReturnCode("200");
        subscribeResp.setMessage("成功");
        return subscribeResp;
    }

    @GetMapping("/express/company")
    @ApiOperation(value = "所有物流公司")
    public ResponseHelper<List<ExpressCompany>> companyList(
    ) throws IOException, RemoteServiceException {
        List<ExpressCompany> expressCompanies = expressLogic.expressCompanyList();
        return ResponseHelper.success(expressCompanies);
    }


    @GetMapping("/region/list")
    @ApiOperation(value = "地区列表")
    public ResponseHelper<List<Region>> regionList(
            @ApiParam(value = "父id(第一级传0)",required = true) @RequestParam(required = true) Long pid
    ) throws RemoteServiceException {
        List<Region> list = commonLogic.queryRegionList(pid);
        return ResponseHelper.success(list);
    }

    @ApiNotAuth
    @ApiOperation("获取获取前往小程序链接")
    @GetMapping("/getUrlScheme")
    public ResponseHelper<String> getUrlScheme(
            @ApiParam(value = "通过 scheme 码进入小程序时的 query",required = false) @RequestParam(required = false) String query,
            @ApiParam(required = false) @RequestParam(required = false) String companyWechatId
    ) throws IOException, WxErrorException {
        return ResponseHelper.success(commonLogic.getUrlScheme(query,companyWechatId));
    }

    @ApiNotAuth
    @GetMapping("/img/get")
    @ApiOperation(value = "获取图片")
    public void getImg(
            @ApiParam(required = true, value = "附件key") @RequestParam(required = true) String key,
            HttpServletResponse response
    ) throws IOException, RemoteServiceException {
        String file = commonLogic.getFile(key, response);
        response.sendRedirect(file);
    }
}
