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

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.admin.AdminDeptWebsitVO;
import com.gree.mall.manager.bean.admin.AdminWebsitTree;
import com.gree.mall.manager.bean.admin.AdminWebsitVO;
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.AdminZone;
import com.gree.mall.manager.plus.service.AdminWebsitService;
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 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.io.IOException;
import java.rmi.RemoteException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "商家管理", tags ={"商家管理"} )
@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);
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "商家管理")
    public ResponseHelper<IPage<AdminWebsitVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<AdminWebsitVO> adminDeptVOIPage = adminWebsitLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(adminDeptVOIPage, new TypeReference<AdminWebsitVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "商家管理导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<AdminWebsitVO> adminDeptVOIPage = adminWebsitLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(adminDeptVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }



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

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

    @PostMapping("/update")
    @ApiOperation("修改商家")
    public ResponseHelper update(@RequestBody AdminWebsit 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<AdminWebsit> detail(@RequestParam String id){
        AdminWebsit detail = adminWebsitLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/import")
    @ApiOperation("导入商家附件(商家导入.xlsx)")
    public ResponseHelper importData(
            @RequestPart("file") MultipartFile file
            , HttpServletRequest request
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        adminWebsitLogic.importData(objects,request);
        return ResponseHelper.success();
    }


}
