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

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.workorder.*;
import com.gree.mall.manager.bean.workorder.DispatchCherVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.workorder.ChangeOrderLogic;
import com.gree.mall.manager.plus.entity.WebsitDispatch;
import com.gree.mall.manager.utils.CommonUtils;
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 javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "自动派工配置", tags = {"自动派工配置"})
@RequestMapping(value = "/dispatch", produces = "application/json; charset=utf-8")
public class DispatchController {

    @Autowired
    private ChangeOrderLogic changeOrderLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "未配置街道-列表")
    public ResponseHelper<Page<DispatchVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<DispatchVO> orderBaseVOIPage = changeOrderLogic.list(zfireParamBean);
        return ResponseHelper.success(orderBaseVOIPage, new TypeReference<DispatchVO>() {
        });
    }


    @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<DispatchVO> page = changeOrderLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/save")
    @ApiOperation(value = "未配置街道-配置")
    public ResponseHelper save(@Valid @RequestBody WebsitDispatch websitDispatch) {
        changeOrderLogic.saveOrder(websitDispatch);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("/listDispatch")
    @ApiOperation(value = "自动派工街道配置-列表")
    public ResponseHelper<Page<WebsitDispatchVO>> listDispatch(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitDispatchVO> orderBaseVOIPage = changeOrderLogic.listDispatch(zfireParamBean);
        return ResponseHelper.success(orderBaseVOIPage, new TypeReference<WebsitDispatchVO>() {
        });
    }


    @PostMapping("/listDispatch/export")
    @ApiOperation(value = "自动派工街道配置-导出")
    public void listDispatchExport(@RequestBody ZfireParamBean zfireParamBean,
                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WebsitDispatchVO> page = changeOrderLogic.listDispatch(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/saveDispatch")
    @ApiOperation(value = "自动派工街道配置-新增")
    public ResponseHelper save(@Valid @RequestBody WebsitDispatchAdd websitDispatchAdd) {
        changeOrderLogic.saveDispatch(websitDispatchAdd);
        return ResponseHelper.success();
    }

    @PostMapping("/updateDispatch")
    @ApiOperation(value = "自动派工街道配置-编辑")
    public ResponseHelper updateDispatch(@Valid @RequestBody WebsitDispatch websitDispatch) {
        changeOrderLogic.updateDispatch(websitDispatch);
        return ResponseHelper.success();
    }


    @PostMapping("/detailDispatch")
    @ApiOperation(value = "自动派工街道配置-详情")
    public ResponseHelper<WebsitDispatch> detailDispatch(@ApiParam(value = "id", required = false) @RequestParam String id) {
        WebsitDispatch websitDispatch = changeOrderLogic.detailDispatch(id);
        return ResponseHelper.success(websitDispatch);
    }


    @PostMapping("/delDispatch")
    @ApiOperation(value = "自动派工街道配置-删除")
    public ResponseHelper delDispatch(@ApiParam(value = "id", required = false) @RequestParam List<String> id) {
        changeOrderLogic.delDispatch(id);
        return ResponseHelper.success();
    }


    @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 {
        changeOrderLogic.importData(file);
        return ResponseHelper.success();
    }

    @PostMapping("/pList")
    @ApiOperation(value = "省")
    public ResponseHelper<List<DispatchCherVO>> pList(
            @RequestBody WebsitDispatchRe websitDispatchRe

    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic.pList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/cList")
    @ApiOperation(value = "市")
    public ResponseHelper<List<DispatchCherVO>> cList(
            @RequestBody WebsitDispatchRe websitDispatchRe

    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic.cList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/aList")
    @ApiOperation(value = "区")
    public ResponseHelper<List<DispatchCherVO>> aList(
            @RequestBody WebsitDispatchRe websitDispatchRe

    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic.aList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/sList")
    @ApiOperation(value = "街道")
    public ResponseHelper<List<DispatchCherVO>> sList(
            @RequestBody WebsitDispatchRe websitDispatchRe
    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic.sList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }


    @PostMapping("/channel")
    @ApiOperation(value = "工单渠道")
    public ResponseHelper<List<DispatchCherVO>> channel(
            @RequestBody WebsitDispatchRe websitDispatchRe
    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic.channel(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());

        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/smallList")
    @ApiOperation(value = "工单类型")
    public ResponseHelper<List<DispatchCherVO>> smallList(
            @RequestBody WebsitDispatchRe websitDispatchRe
    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic
                .smallList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                        websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                        websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());

        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/categoryList")
    @ApiOperation(value = "产品大类")
    public ResponseHelper<List<DispatchCherVO>> categoryList(
            @RequestBody WebsitDispatchRe websitDispatchRe
    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic
                .categoryList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                        websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                        websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }

    @PostMapping("/sourceList")
    @ApiOperation(value = "工单来源")
    public ResponseHelper sourceList(
            @RequestBody WebsitDispatchRe websitDispatchRe
    ) {
        List<DispatchCherVO> dispatchCherVOS = changeOrderLogic
                .sourceList(websitDispatchRe.getProvinceCode(), websitDispatchRe.getCityCode(),
                        websitDispatchRe.getAreaCode(), websitDispatchRe.getStreetCode(), websitDispatchRe.getDictCode(), websitDispatchRe.getOrderSmallId(),
                        websitDispatchRe.getCategoryId(), websitDispatchRe.getOrderSmallId());
        return ResponseHelper.success(dispatchCherVOS);
    }


}
