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

import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.admin.AdminWebsitNewBean;
import com.gree.mall.manager.bean.admin.AdminWebsitTree;
import com.gree.mall.manager.bean.workorder.AdminWebsitList;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.admin.AdminWebsitLogic;
import com.gree.mall.manager.plus.entity.AdminWebsit;
import com.gree.mall.manager.plus.entity.AdminWebsitPayConfig;
import com.gree.mall.manager.plus.entity.AdminZone;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
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.util.List;

@Slf4j
@RestController
@Api(value = "企业微信部门管理API", tags = {"企业微信部门管理API"})
@RequestMapping(value = "/admin/websit", produces = "application/json; charset=utf-8")
public class AdminWebsitController {

    @Autowired
    AdminWebsitLogic adminWebsitLogic;

    @GetMapping("/tree")
    @ApiOperation("部门树")
    public ResponseHelper<AdminWebsitTree> tree(HttpServletRequest request) {
        List<AdminWebsitTree> tree = adminWebsitLogic.tree(request);
        return ResponseHelper.success(tree);
    }

    @GetMapping("/list")
    @ApiOperation("部门列表")
    public ResponseHelper<List<AdminWebsit>> list(
            @ApiParam(value = "A=平台 B=商户网点 C=普通网点") @RequestParam(required = false) String type,
            @RequestParam(required = false) Boolean isAll,
            @RequestParam(required = false) Boolean status,
            @RequestParam(required = false) Boolean isIncre,
            @RequestParam(required = false) String streetCode,
            @RequestParam(required = false) String channelId,
            @RequestParam(required = false) String categoryId,
            @RequestParam(required = false) String orderSourceId,
            @RequestParam(required = false) String orderSmallId,
            @RequestParam(required = false) Boolean queryPartsWebsit
    ) {
        List<AdminWebsit> list = adminWebsitLogic.list(type, isAll, isIncre, status, streetCode, channelId, categoryId, orderSmallId, orderSourceId, queryPartsWebsit);
        return ResponseHelper.success(list);
    }


    @GetMapping("/websitList")
    @ApiOperation("网点列表")
    public ResponseHelper<List<AdminWebsitList>> websitList(
            @ApiParam(value = "A=平台 B=商户网点 C=普通网点") @RequestParam(required = false) String type,
            @ApiParam(value = "当前权限网点") @RequestParam(required = false) Boolean isAll,
            @ApiParam(value = "状态") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "是否增值网点") @RequestParam(required = false) Boolean isIncre,
            @ApiParam(value = "街道id") @RequestParam(required = false) String streetCode,
            @ApiParam(value = "渠道") @RequestParam(required = false) String channelId,
            @ApiParam(value = "产品大类") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "工单来源") @RequestParam(required = false) String orderSource,
            @ApiParam(value = "工单类型") @RequestParam(required = false) String orderSmallId
    ) {
        List<AdminWebsitList> list = adminWebsitLogic.websitList(type, isAll, isIncre, status, streetCode, channelId, categoryId, orderSmallId, orderSource);
        return ResponseHelper.success(list);
    }

    @GetMapping("/zone/list")
    @ApiOperation("区域列表")
    public ResponseHelper<AdminZone> zoneList() {
        List<AdminZone> adminZones = adminWebsitLogic.adminZoneList();
        return ResponseHelper.success(adminZones);
    }

    @PostMapping("/add")
    @ApiOperation("新增部门")
    public ResponseHelper add(@RequestBody AdminWebsitNewBean adminWebsit, HttpServletRequest request) throws RemoteServiceException, WxErrorException {
        adminWebsitLogic.add(adminWebsit, request);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改部门")
    public ResponseHelper update(@RequestBody AdminWebsitNewBean adminWebsit, HttpServletRequest request) throws RemoteServiceException, WxErrorException {
        adminWebsitLogic.update(adminWebsit, request);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除部门")
    public ResponseHelper delete(@RequestParam String id, HttpServletRequest request) throws WxErrorException {
        adminWebsitLogic.delete(id, request);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation("部门详情")
    public ResponseHelper<AdminWebsitNewBean> detail(@RequestParam String id) {
        AdminWebsitNewBean detail = adminWebsitLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/save/pay/config")
    @ApiOperation("新增编辑网点支付配置")
    public ResponseHelper savePayConfig(@RequestBody AdminWebsitPayConfig payConfig, HttpServletRequest request) throws RemoteServiceException {
        adminWebsitLogic.savePayConfig(payConfig, request);
        return ResponseHelper.success();
    }

    @PostMapping("/delete/pay/config")
    @ApiOperation("删除网点支付配置")
    public ResponseHelper deletePayConfig(@RequestParam String id, HttpServletRequest request) {
        adminWebsitLogic.deletePayConfig(id, request);
        return ResponseHelper.success();
    }


    @PostMapping("/pay/config/list")
    @ApiOperation("网点支付配置列表")
    public ResponseHelper<List<AdminWebsitPayConfig>> payConfigList(
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "类别 M=辅材 P=配件") @RequestParam(required = false) String type,
            @ApiParam(value = "状态 true=开启 false=关闭") @RequestParam(required = false) Boolean status
    ) {
        List<AdminWebsitPayConfig> list = adminWebsitLogic.payConfigList(websitId, type, status);
        return ResponseHelper.success(list);
    }

    @PostMapping("/import")
    @ApiOperation(value = "网点管理-导入(下载模板:网点管理导入模板.xlsx)")
    public ResponseHelper<String> importData(
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        adminWebsitLogic.importData(file);
        return ResponseHelper.success();
    }

    @GetMapping("/export")
    @ApiOperation(value = "网点管理-导出")
    public void export(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ExcelData excelData = adminWebsitLogic.exportData();
        ExcelUtils.exportExcel(request, response, "网点管理列表.xlsx", excelData);
    }
}
