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


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.supplier.KingDeeSupplierVO;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3SupplierLogic;
import com.zfire.mall.manager.plus.entity.KingDeeSupplier;
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;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "供应商", tags ={"供应商管理"} )
@RequestMapping(value = "/supplier", produces = "application/json; charset=utf-8")
public class K3SupplierController {


    @Autowired
    K3SupplierLogic k3SupplierLogic;

//    @ApiOperation(value = "同步供应商")
//    @PostMapping("/syncSupplier")
//    public ResponseHelper syncSupplier(
//            @ApiParam(required = true, value = "组织机构") @RequestParam(required = true) String orgId,
//            @ApiParam(required = true, value = "组织机构编码") @RequestParam(required = true) String orgNumber,
//            @ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String startTime,
//            @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime
//    ) throws Exception {
//        k3SupplierLogic.allSync(orgId,orgNumber,startTime,endTime);
//        return ResponseHelper.success();
//    }


    @ApiOperation(value = "供应商列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<KingDeeSupplier>> list(
            @ApiParam(required = false, value = "供应商编码") @RequestParam(required = false) String number,
            @ApiParam(required = false, value = "供应商名称") @RequestParam(required = false) String name,
            @ApiParam(required = false, value = "使用组织编码") @RequestParam(required = false) String useOrgNumber,
            @ApiParam(required = false, value = "使用组织名称") @RequestParam(required = false) String useOrgName,
            @ApiParam(required = false, value = "简称") @RequestParam(required = false) String shortName,
            @ApiParam(required = false, value = "供应商分组") @RequestParam(required = false) String groupName,
            @ApiParam(required = false, value = "审核人") @RequestParam(required = false) String auditName,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeeSupplier> kingDeeSupplier = k3SupplierLogic.list(number,name,useOrgNumber,useOrgName,shortName,groupName,auditName,pageNum,pageSize);
        return ResponseHelper.success(kingDeeSupplier);
    }

    @ApiOperation(value = "供应商详情")
    @GetMapping("/detail")
    public ResponseHelper<KingDeeSupplier> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        KingDeeSupplier kingDeeSupplier = k3SupplierLogic.detail(id);
        return ResponseHelper.success(kingDeeSupplier);
    }

    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("供应商-列表V2")
    public ResponseHelper<IPage<KingDeeSupplierVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<KingDeeSupplierVO> list = k3SupplierLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<KingDeeSupplierVO>() {});
    }

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