package com.dsk.jsk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.dsk.biz.utils.ExcelUtils;
import com.dsk.common.constant.Constants;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.core.service.OssService;
import com.dsk.jsk.domain.JskCombineBidPageDto;
import com.dsk.jsk.domain.JskCombineCertificateDto;
import com.dsk.jsk.domain.JskCombineCountDto;
import com.dsk.jsk.domain.JskCombineSearchDto;
import com.dsk.jsk.domain.vo.JskCombineBidProjectExportVo;
import com.dsk.jsk.domain.vo.JskCombineWinBidProjectExportVo;
import com.dsk.jsk.service.JskCombineInfoService;
import com.dsk.oss.core.OssClient;
import com.dsk.oss.entity.UploadResult;
import com.dsk.oss.factory.OssFactory;
import com.dsk.system.domain.vo.SysOssVo;
import com.dsk.system.service.ISysOssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 集团户相关信息
 *
 * @author lcl
 * @create 2023/8/7
 */
@Slf4j
@RestController
@RequestMapping(value = "/combine/info")
public class JskCombineInfoController extends BaseController {

    @Autowired
    private JskCombineInfoService baseService;
    @Autowired
    private ISysOssService ossService;

    /**
     * 集团成员列表
     */
    @PostMapping("/memberList")
    public TableDataInfo memberList(@RequestBody JskCombineSearchDto dto) throws Exception {
        return baseService.memberList(dto);
    }

    /**
     * 分组成员数量
     */
    @PostMapping("/group/memberCount")
    public R groupMemberCount(@RequestBody JskCombineSearchDto dto) {
        return baseService.groupMemberCount(dto);
    }

    /**
     * 集团业绩列表
     */
    @PostMapping("/businessList")
    public TableDataInfo businessList(@RequestBody JskCombineSearchDto dto) throws Exception {
        return baseService.businessList(dto);
    }

    /**
     * 集团资质列表
     */
    @PostMapping("/certificateList")
    public TableDataInfo certificateList(@RequestBody JskCombineSearchDto dto) throws Exception {
        return baseService.certificateList(dto);
    }

    /**
     * 集团成员资质列表
     */
    @PostMapping("/member/certificateList")
    public TableDataInfo menberCertificateList(@RequestBody JskCombineCertificateDto dto) throws Exception {
        return baseService.menberCertificateList(dto);
    }

    /**
     * 集团资质分组统计
     */
    @PostMapping("/group/certificateCount")
    public R groupCertificateCount(@RequestBody JskCombineSearchDto dto) throws Exception {
        return baseService.groupCertificateCount(dto);
    }

    /**
     * 集团户搜索
     */
    @PostMapping("/index")
    public AjaxResult index(@RequestBody Map<String, Object> object) {
        return baseService.index(object);
    }

    /**
     * 查央企
     */
    @PostMapping("/centralEnterprse")
    public AjaxResult centralEnterprse() {
        return baseService.centralEnterprse();
    }

    /**
     * 查央企子公司
     */
    @PostMapping("/centralEnterprse/child")
    public AjaxResult centralEnterprseChild() {
        return baseService.centralEnterprseChild();
    }

    /**
     * 查央企子公司  翻页
     */
    @PostMapping("/centralEnterprse/child/page")
    public AjaxResult centralEnterprseChildPage(@RequestBody Map<String, Object> object) {
        return baseService.centralEnterprseChildPage(object);
    }

    /**
     * 查地方国企
     */
    @PostMapping("/centralEnterprse/local")
    public AjaxResult centralEnterprseLocal(@RequestBody Map<String, Object> object) {
        return baseService.centralEnterprseLocal(object);
    }

    /**
     * 查地方国企 翻页
     */
    @PostMapping("/centralEnterprse/local/page")
    public AjaxResult centralEnterprseLocalPage(@RequestBody Map<String, Object> object) {
        return baseService.centralEnterprseLocalPage(object);
    }

    /**
     * 民营企业
     */
    @PostMapping("/centralEnterprse/social")
    public AjaxResult centralEnterprseSocial() {
        return baseService.centralEnterprseSocial();
    }

    /**
     * 民营企业  翻页
     */
    @PostMapping("/centralEnterprse/social/page")
    public AjaxResult centralEnterprseSocialPage(@RequestBody Map<String, Object> object) {
        return baseService.centralEnterprseSocialPage(object);
    }


    /**
     * 导出中标业绩
     */
    @PostMapping("/export/win/bid")
    @SaCheckPermission("combine:info:export:win:bid")
    public void exportWinBid(@RequestBody JskCombineSearchDto dto, HttpServletResponse response) {
        List<JskCombineWinBidProjectExportVo> list = baseService.exportWinBid(dto);
        ExcelUtils<JskCombineWinBidProjectExportVo> util = new ExcelUtils<>(JskCombineWinBidProjectExportVo.class);
        util.exportExcel(response, list, "集团中标", dto.getCombineName().concat("中标业绩清单"), true);
//        String fileName = util.localInit(list, "集团中标", dto.getCombineName().concat("中标业绩清单"), true);
    }

    /**
     * 导出招标业绩
     */
    @PostMapping("/export/bid")
    @SaCheckPermission("combine:info:export:bid")
    public void exportBid(@RequestBody JskCombineBidPageDto dto, HttpServletResponse response) {
        List<JskCombineBidProjectExportVo> list = baseService.exportBid(dto);
        ExcelUtils<JskCombineBidProjectExportVo> util = new ExcelUtils<>(JskCombineBidProjectExportVo.class);
        util.exportExcel(response, list, "集团招标", dto.getCombineName().concat("招标公告清单"), true);
    }


    /**
     * 集团招标分页列表
     */
    @PostMapping("/bidPage")
    public TableDataInfo bidPage(@RequestBody JskCombineBidPageDto dto) throws Exception {
        return baseService.bidPage(dto);
    }


    /**
     * 导出上传oss测试
     */
    @PostMapping("/export/upload")
    public R<SysOssVo> exportuUpload(@RequestBody JskCombineSearchDto dto) {
        List<JskCombineWinBidProjectExportVo> list = baseService.exportWinBid(dto);
        String title = dto.getCombineName().concat("中标业绩清单");
        ExcelUtils<JskCombineWinBidProjectExportVo> util = new ExcelUtils<>(JskCombineWinBidProjectExportVo.class);
        ByteArrayOutputStream ba = util.exportExcel(list, "集团中标", title, true);
        ByteArrayInputStream bio = new ByteArrayInputStream(ba.toByteArray());
        OssClient client = OssFactory.instance();
        UploadResult uploadResult = client.uploadSuffix(bio, Constants.SUFFIX_XLSX, Constants.CONTENT_TYPE_XLSX);
        //总文件记录
        SysOssVo sysOssVo = ossService.buildResultEntity(title.concat(Constants.SUFFIX_XLSX), Constants.SUFFIX_XLSX, client.getConfigKey(), uploadResult);
        return R.ok(sysOssVo);
    }

    /***
     *@Description: 获取集团logo
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/9/12 16:05
     */
    @RequestMapping(value = "/combineMemberLogo", method = RequestMethod.POST)
    public R combineMemberLogo(@RequestBody Map<String,Object> paramMap) {
        return baseService.combineMemberLogo(paramMap);
    }

    /***
     *@Description: 集团统计展示调用允许
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/9/12 16:05
     */
    @RequestMapping(value = "/memberCount", method = RequestMethod.POST)
    public R memberCount(@RequestBody JskCombineCountDto dto) throws Exception {
        return baseService.memberCount(dto);
    }
}
