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

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.letter.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.LetterLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
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 java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "工作联络函", tags = {"工作联络函"})
@RequestMapping(value = "/letterProcess", produces = "application/json; charset=utf-8")
public class LetterProcessController {

    @Autowired
    private LetterLogic letterLogic;



    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工作联络函列表")
    public ResponseHelper<IPage<LeLetterVO>> listLetter(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<LeLetterVO> policyOrderVOIPage = letterLogic.listLetter(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<LeLetterVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "工作联络函导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<LeLetterVO> policyOrderVOIPage = letterLogic.listLetter(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/detailLetter")
    @ApiOperation("工作联络函详情")
    public ResponseHelper<LeLetterDetail> detailLetter(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        LeLetterDetail leLetter = letterLogic.detailLetter(id);
        return ResponseHelper.success(leLetter);
    }

    @PostMapping("/addLetter")
    @ApiOperation("新增工作联络函")
    public ResponseHelper addLetter(
            @RequestBody LeLetter leLetter,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.addLetter(leLetter);
        return ResponseHelper.success();
    }

    @PostMapping("/updateLetter")
    @ApiOperation("工作联络函修改")
    public ResponseHelper updateLetter(
            @RequestBody LeLetter leLetter,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.updateLetter(leLetter);
        return ResponseHelper.success();
    }


    @PostMapping("/delLetter")
    @ApiOperation("工作联络函删除")
    public ResponseHelper delLetter(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.delLetter(id);
        return ResponseHelper.success();
    }


    @PostMapping("/send")
    @ApiOperation("发送")
    public ResponseHelper send(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.send(ids);
        return ResponseHelper.success();
    }


    @PostMapping("/cancel")
    @ApiOperation("作废")
    public ResponseHelper cancel(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.cancel(ids);
        return ResponseHelper.success();
    }

    @PostMapping("/reject")
    @ApiOperation("驳回")
    public ResponseHelper reject(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.reject(ids);
        return ResponseHelper.success();
    }


    @PostMapping("/appeal")
    @ApiOperation("网点申诉")
    public ResponseHelper appeal(
            @RequestBody LeLetterItem leLetterItem,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.appeal(leLetterItem);
        return ResponseHelper.success();
    }



    @PostMapping("/examine")
    @ApiOperation("中心批复")
    public ResponseHelper examine(
            @RequestBody LeLetterItem leLetterItem,
            HttpServletRequest request
    ) throws ParseException {
        letterLogic.examine(leLetterItem);
        return ResponseHelper.success();
    }


}
