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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.charging.ChargingStandardBean;
import com.gree.mall.manager.bean.listvo.ChargingStandard2VO;
import com.gree.mall.manager.bean.listvo.ChargingStandardTypeVO;
import com.gree.mall.manager.bean.listvo.ChargingStandardVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.charging.ChargingStandardLogic;
import com.gree.mall.manager.plus.entity.ChargingStandard;
import com.gree.mall.manager.plus.entity.ChargingStandardType;
import com.gree.mall.manager.utils.CommonUtils;
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.util.List;

@Slf4j
@RestController
@Api(value = "收费标准和故障排查API", tags ={"收费标准和故障排查API"} )
@RequestMapping(value = "/charging/standard", produces = "application/json; charset=utf-8")
public class ChargingStandardController {

    @Autowired
    ChargingStandardLogic chargingStandardLogic;


    @ZfireList
    @PostMapping("/type/list2")
    @ApiOperation("收费标准类型-列表v2")
    public ResponseHelper<IPage<ChargingStandardTypeVO>> typeList2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<ChargingStandardTypeVO> chargingStandardTypeVOIPage = chargingStandardLogic.listType2(zfireParamBean);
        return ResponseHelper.success(chargingStandardTypeVOIPage, new TypeReference<ChargingStandardTypeVO>() {});
    }


    @GetMapping("/type/list")
    @ApiOperation("收费标准类型-列表")
    public ResponseHelper<List<ChargingStandardType>> typeList(){
        List<ChargingStandardType> chargingStandardTypes = chargingStandardLogic.listType();
        return ResponseHelper.success(chargingStandardTypes);
    }

    @PostMapping("/type/save")
    @ApiOperation("收费标准类型-保存")
    public ResponseHelper saveType(
            @RequestBody ChargingStandardType chargingStandardType,
            HttpServletRequest request
    ){
        chargingStandardLogic.saveType(chargingStandardType,request);
        return ResponseHelper.success();
    }

    @PostMapping("/type/delete")
    @ApiOperation("收费标准类型-删除")
    public ResponseHelper deleteType(
            @RequestParam String id,
            HttpServletRequest request
    ){
        chargingStandardLogic.deleteType(id);
        return ResponseHelper.success();
    }

    @GetMapping("/type/detail")
    @ApiOperation("收费标准类型-详情")
    public ResponseHelper<ChargingStandardType> saveType(
            @RequestParam String id
    ){
        ChargingStandardType chargingStandardType = chargingStandardLogic.detailType(id);
        return ResponseHelper.success(chargingStandardType);
    }


    @GetMapping("/list")
    @ApiOperation("收费标准/故障排查-列表")
    public ResponseHelper<ChargingStandardBean> list(
            @ApiParam("1=收费标准 2=故障排查") @RequestParam Integer type,
            @ApiParam(value = "状态true=正常 false=禁用",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "品牌id",required = false) @RequestParam(required = false) String brandId,
            @ApiParam(value = "一级分类id",required = false) @RequestParam(required = false) String oneCategoryId,
            @ApiParam(value = "二级分类id",required = false) @RequestParam(required = false) String twoCategoryId,
            @ApiParam(value = "二级分类id",required = false) @RequestParam(required = false) String content1,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ){
        IPage<ChargingStandardBean> list = chargingStandardLogic
                .list(type,status,brandId,oneCategoryId,twoCategoryId,content1,pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ZfireList
    @PostMapping("/list2")
    @ApiOperation("收费标准V2-列表")
    public ResponseHelper<IPage<ChargingStandardVO>> list2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<ChargingStandardVO> chargingStandardVOIPage = chargingStandardLogic.list2(zfireParamBean);
        return ResponseHelper.success(chargingStandardVOIPage, new TypeReference<ChargingStandardVO>(){});
    }

    @ZfireList
    @PostMapping("/list3")
    @ApiOperation("故障现象V2-列表")
    public ResponseHelper<IPage<ChargingStandard2VO>> list3(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<ChargingStandard2VO> chargingStandardVOIPage = chargingStandardLogic.list3(zfireParamBean);
        return ResponseHelper.success(chargingStandardVOIPage, new TypeReference<ChargingStandard2VO>(){});
    }

    @PostMapping("/list3/pageExport")
    @ApiOperation("故障现象V2-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<ChargingStandard2VO> list = chargingStandardLogic.list3(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @PostMapping("/save")
    @ApiOperation("收费标准/故障排查-保存")
    public ResponseHelper save(
            @RequestBody ChargingStandard chargingStandard,
            HttpServletRequest request
    ){
        chargingStandardLogic.save(chargingStandard,request);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("收费标准/故障排查-删除")
    public ResponseHelper delete(
            @RequestParam String id,
            HttpServletRequest request
    ){
        chargingStandardLogic.delete(id);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation("收费标准/故障排查-详情")
    public ResponseHelper<ChargingStandardBean> detail(
            @RequestParam String id
    ){
        ChargingStandardBean detail = chargingStandardLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @GetMapping("/download")
    @ApiOperation("故障排查-下载")
    public void download(HttpServletResponse response) throws IOException {
        CommonUtils.downloadFile("故障排查.xlsx",response);
    }


    @PostMapping("/import")
    @ApiOperation("故障排查-导入")
    public ResponseHelper importData(@RequestParam MultipartFile file) throws IOException {
        chargingStandardLogic.importData(file);
        return ResponseHelper.success();
    }


    @GetMapping("/exportData")
    @ApiOperation("收费标准/故障排查-列表")
    public void exportData(
            @ApiParam("1=收费标准 2=故障排查") @RequestParam Integer type,
            @ApiParam(value = "状态true=正常 false=禁用",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "品牌id",required = false) @RequestParam(required = false) String brandId,
            @ApiParam(value = "一级分类id",required = false) @RequestParam(required = false) String oneCategoryId,
            @ApiParam(value = "二级分类id",required = false) @RequestParam(required = false) String twoCategoryId,
            @ApiParam(value = "二级分类id",required = false) @RequestParam(required = false) String content1,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<ChargingStandardBean> list = chargingStandardLogic.list(type,status,brandId,oneCategoryId,twoCategoryId,content1,1, -1);
        ExcelData excelData = chargingStandardLogic.exportData(list.getRecords());
        ExcelUtils.exportExcel(request,response,"info.xlsx",excelData);
    }


}
