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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.ums.UmsSupplementRecordVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.ums.FileSftpUploaderLogic;
import com.gree.mall.manager.logic.ums.SupplementRecordLogic;
import com.gree.mall.manager.plus.entity.UmsRegionCode;
import com.gree.mall.manager.plus.entity.UmsSupplementRecord;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@Api(value = "粤焕新补贴管理", tags = {"粤焕新补贴管理"})
@RequestMapping(value = "/ums/supplement", produces = "application/json; charset=utf-8")
public class SupplementRecordController {

    @Resource
    SupplementRecordLogic supplementRecordLogic;
    @Resource
    FileSftpUploaderLogic fileSftpUploaderLogic;

    @PostMapping("/generate")
    @ApiOperation("生成记录")
    public ResponseHelper<String> generate(
            @ApiParam(value = "订单id", required = true) @RequestParam String orderId
    ) {
        String id = supplementRecordLogic.generateSupplementRecord(orderId);
        return ResponseHelper.success(id);
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<UmsSupplementRecordVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<UmsSupplementRecordVO> adminDeptVOIPage = supplementRecordLogic.list(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return ResponseHelper.success(adminDeptVOIPage, new TypeReference<UmsSupplementRecordVO>() {});
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<UmsSupplementRecord> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        UmsSupplementRecord detail = supplementRecordLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/submit")
    @ApiOperation("提交")
    public ResponseHelper submit(
            @RequestBody UmsSupplementRecord record
    ) {
        supplementRecordLogic.submit(record);
        return ResponseHelper.success();
    }

    @PostMapping("/region/code/list")
    @ApiOperation("补贴区域码列表")
    public ResponseHelper<List<UmsRegionCode>> regionCodeList(
            @ApiParam(value = "省code") @RequestParam(required = false) String provinceCode,
            @ApiParam(value = "市code") @RequestParam(required = false) String cityCode,
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<UmsRegionCode> list = supplementRecordLogic.regionCodeList(request, provinceCode, cityCode);
        return ResponseHelper.success(list);
    }



    @GetMapping("/upload/test")
    @ApiOperation("上传测试")
    public ResponseHelper<String> uploadTest(
            @ApiParam(value = "img") @RequestParam(required = false) String url
    ) throws Exception {
        String name = fileSftpUploaderLogic.uploadFromUrl(url, "GEJLS", "o2o_GEJLS", "5=*kIK6bQ2");
        return ResponseHelper.success(name);
    }
}
