package com.gree.mall.manager.controller;

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.Freight.FreightTemplateBean;
import com.gree.mall.manager.bean.Freight.RegionTreeBean;
import com.gree.mall.manager.bean.FreightTemplateVO;
import com.gree.mall.manager.bean.carouselMap.CarouselMapVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.FreightLogic;
import com.gree.mall.manager.plus.entity.FreightTemplate;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "运费管理", tags ={"运费管理"} )
@RequestMapping(value = "/freight", produces = "application/json; charset=utf-8")
public class FreightController {

    @Autowired
    FreightLogic freightLogic;


    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "运费模板列表V2")
    public ResponseHelper<IPage<FreightTemplateVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<FreightTemplateVO> page = freightLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<FreightTemplateVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "运费模板列表V2导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<FreightTemplateVO> page = freightLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }




    @GetMapping("/list")
    @ApiOperation(value = "运费模板列表")
    public ResponseHelper<Page<FreightTemplate>> page(
            HttpServletRequest request,
            @ApiParam(value = "名称",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "状态(true：启用 false:禁用",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<FreightTemplate> list = freightLogic.list(request,name, status, startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "运费模板详情")
    public ResponseHelper<FreightTemplateBean> detail(
            @ApiParam(value = "模板id",required = true) @RequestParam(required = true) String freightTemplateId
    ){
        FreightTemplateBean detail = freightLogic.detail(freightTemplateId);
        return ResponseHelper.success(detail);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除运费模板")
    public ResponseHelper delete(
            @ApiParam(value = "模板id",required = true) @RequestParam(required = true) String freightTemplateId
    ){
        freightLogic.delete(freightTemplateId);
        return ResponseHelper.success();
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增修改运费模板")
    public ResponseHelper delete(
            @ApiParam(value = "模板",required = true) @RequestBody(required = true) FreightTemplateBean freightTemplateBean,
            HttpServletRequest request
    ){
        freightLogic.save(freightTemplateBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation(value = "修改运费模板状态")
    public ResponseHelper updateStatus(
            @ApiParam(value = "模板id",required = true) @RequestParam(required = true) String freightTemplateId,
            @ApiParam(value = "状态:true=启用 false=禁用",required = true) @RequestParam(required = true) Boolean status
    ){
        freightLogic.updateStatus(freightTemplateId,status);
        return ResponseHelper.success();
    }


    @GetMapping("/region/tree")
    @ApiOperation(value = "获取地区树")
    public ResponseHelper<List<RegionTreeBean>> regionTree(
            @ApiParam(value = "等级(1=省，2=市，3=区，4=街道，5=社区)(默认为区以上)",required = false) @RequestParam(required = false) Integer level
    ){
        List<RegionTreeBean> regionTreeBeans = freightLogic.regionTree(level);
        return ResponseHelper.success(regionTreeBeans);
    }

}
