package com.zfire.mall.manager.controller.saletype;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.listV2.PolicyVO;
import com.zfire.mall.manager.bean.listV2.SaleTypeVO;
import com.zfire.mall.manager.bean.listV2.WalletRebateVO;
import com.zfire.mall.manager.bean.listV2.dto.PolicyDTO;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.saletype.SaleTypeLogic;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "销售类型API", tags ={"销售类型API"} )
@RequestMapping(value = "/sale/type", produces = "application/json; charset=utf-8")
public class SaleTypeController {

    @Autowired
    SaleTypeLogic saleTypeLogic;


    @ZfireList
    @PostMapping("/rebate/list/v2")
    @ApiOperation("销售类型列表V2")
    public ResponseHelper<IPage<SaleTypeVO>> customerListV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<SaleTypeVO> saleTypeVOIPage = saleTypeLogic.listV2(zfireParamBean);
        return ResponseHelper.success(saleTypeVOIPage, new TypeReference<SaleTypeVO>() {});
    }

    @PostMapping("/list/v2/export")
    @ApiOperation("销售类型列表V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SaleTypeVO> list = saleTypeLogic.listV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<SaleType>> list(
            //String saleCode,String saleName,Boolean status,Integer pageNum,Integer pageSize
            @ApiParam(required = false, value = "销售类型编码") @RequestParam(required = false) String saleCode,
            @ApiParam(required = false, value = "销售类型名称") @RequestParam(required = false) String saleName,
            @ApiParam(required = false, value = "状态1=已启用 0=已停用") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<SaleType> list = saleTypeLogic.list(saleCode, saleName,status, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(
            //String saleCode,String saleName,Boolean status,Integer pageNum,Integer pageSize
            @ApiParam(required = false, value = "销售类型编码") @RequestParam(required = false) String saleCode,
            @ApiParam(required = false, value = "销售类型名称") @RequestParam(required = false) String saleName,
            @ApiParam(required = false, value = "状态1=已启用 0=已停用") @RequestParam(required = false) Boolean status,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<SaleType> list = saleTypeLogic.list(saleCode, saleName,status, 1, -1);
        ExcelData export = saleTypeLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售类型.xlsx",export);
    }


    @ApiOperation(value = "详情")
    @GetMapping("/detail")
    public ResponseHelper<SaleType> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        SaleType detail = saleTypeLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public ResponseHelper<SaleType> add(
            @RequestBody(required = true) SaleType saleType
    ) throws Exception {
        saleTypeLogic.add(saleType);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public ResponseHelper<SaleType> update(
            @RequestBody(required = true) SaleType saleType
    ) throws Exception {
        saleTypeLogic.update(saleType);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public ResponseHelper<SaleType> delete(
            @RequestParam(required = true) String id
    ) throws Exception {
        saleTypeLogic.delete(id);
        return ResponseHelper.success();
    }

    @ApiOperation("销售类型-家用工程专用")
    @GetMapping("/home/list")
    public ResponseHelper<List<SaleType>> homeList(){
        List<SaleType> saleTypes = saleTypeLogic.homeList();
        return ResponseHelper.success(saleTypes);
    }

}
