package com.ruoyi.ibs.customerselect.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.customerselect.domain.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ibs.customerselect.service.IMyCustomerService;
import com.ruoyi.ibs.grid.domain.excel.NewPersonAnchor;
import com.ruoyi.ibs.list.service.ISysCampaignGroupCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的客户 控制类
 *
 * @author ruoyi
 */
@Api("客户基本信息管理")
@RestController
@RequestMapping("/system/custBaseInfo")
public class MyCustomerController extends BaseController {

    @Autowired
    private IMyCustomerService myCustomerService;

    @Autowired
    ISysCampaignGroupCustomerService iSysCampaignGroupCustomerService;

    private static Logger logger= LoggerFactory.getLogger(MyCustomerController.class);
    /**
     * 获取我的客户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:custBaseInfo:list')")
    @Log(title = "我的客户-获取客户信息列表")
    @PostMapping("/list")
    @ApiOperation("获取客户信息列表")
    public AjaxResult list(@RequestBody CustBaseInfo sysCustomerBasedata)
//    public TableDataPageInfo<CustBaseInfo> list(CustBaseInfo sysCustomerBasedata)
    {
//        Page<Object> page = startPage();
        List<CustBaseInfo> custBaseInfoList = myCustomerService.selectSysCustomerBasedataList(sysCustomerBasedata);
        logger.info(custBaseInfoList.toString());
//        return getDataTable(custBaseInfoList ,page);
        return AjaxResult.success(custBaseInfoList);
    }

    @Log(title = "我的客户-获取商户云相关信息")
    @GetMapping("/merchantMessage")
    @ApiOperation("获取商户云相关信息")
    public AjaxResult merchantMessage( String  custId)
//    public TableDataPageInfo<CustBaseInfo> list(CustBaseInfo sysCustomerBasedata)
    {
//        Page<Object> page = startPage();
        MerchantMcspInfo merchantMcspInfo = myCustomerService.selectmerchantMessage(custId);
//        logger.info(merchantMcspInfo.toString());
//        return getDataTable(custBaseInfoList ,page);
        return AjaxResult.success(merchantMcspInfo);
    }

    @GetMapping("/searchList")
    @ApiOperation("获取高级筛选项")
    public AjaxResult searchList(CustBaseInfo sysCustomerBasedata) {
        CustListSearchVo custBaseInfoList = myCustomerService.selectCustomListSearchVo(sysCustomerBasedata);
        logger.info(custBaseInfoList.toString());
//        return getDataTable(custBaseInfoList ,page);
        return AjaxResult.success(custBaseInfoList);
    }
    @Log(title = "我的客户-我的客户导出（企业）")
    @PostMapping("/exportBusiness")
    @ApiOperation("我的客户导出（企业）")
    public void exportBusiness(HttpServletResponse response, @RequestBody List<BusinessExportVo> list)
    {
        for (BusinessExportVo b:list
             ) {
            b.setCustPattern("2");
        }
        try {
            ExcelUtil<BusinessExportVo> util = new ExcelUtil<>(BusinessExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Log(title = "我的客户-我的客户导出1000条（企业）")
    @PostMapping("/exportBusiness1000")
    @ApiOperation("我的客户导出1000条（企业）")
    public void exportBusiness1000(HttpServletResponse response, @RequestBody CustBaseInfo sysCustomerBasedata)
    {
        sysCustomerBasedata.setSize(1000);
        sysCustomerBasedata.setNum(1);
        List<CustBaseInfo> custBaseInfoList = myCustomerService.selectSysCustomerBasedataList(sysCustomerBasedata);
        List<BusinessExportVo> list = new ArrayList<>();
        for (CustBaseInfo b:custBaseInfoList
        ) {
            BusinessExportVo businessExportVo = new BusinessExportVo();
            BeanUtils.copyProperties(b,businessExportVo);
            businessExportVo.setCustPattern("2");
            list.add(businessExportVo);
        }
        try {
            ExcelUtil<BusinessExportVo> util = new ExcelUtil<>(BusinessExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "我的客户-我的客户导出（商户）")
    @PostMapping("/exportMerchant")
    @ApiOperation("我的客户导出（商户）")
    public void exportMerchant(HttpServletResponse response, @RequestBody List<MerchantExportVo> list)
    {
        for (MerchantExportVo m:list
        ) {
            m.setCustPattern("1");
        }
        try {
            ExcelUtil<MerchantExportVo> util = new ExcelUtil<>(MerchantExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "我的客户-我的客户导出1000条（商户）")
    @PostMapping("/exportMerchant1000")
    @ApiOperation("我的客户导出1000条（商户）")
    public void exportMerchant1000(HttpServletResponse response, @RequestBody CustBaseInfo sysCustomerBasedata)
    {
        sysCustomerBasedata.setSize(1000);
        sysCustomerBasedata.setNum(1);
        List<CustBaseInfo> custBaseInfoList = myCustomerService.selectSysCustomerBasedataList(sysCustomerBasedata);
        List<MerchantExportVo> list = new ArrayList<>();
        for (CustBaseInfo b:custBaseInfoList
        ) {
            MerchantExportVo merchantExportVo = new MerchantExportVo();
            BeanUtils.copyProperties(b,merchantExportVo);
            merchantExportVo.setCustPattern("1");
            list.add(merchantExportVo);
        }
        try {
            ExcelUtil<MerchantExportVo> util = new ExcelUtil<>(MerchantExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "我的客户-我的客户导出（个人）")
    @PostMapping("/exportRetail")
    @ApiOperation("我的客户导出（个人）")
    public void exportRetail(HttpServletResponse response, @RequestBody List<RetailExportVo> list)
    {
        for (RetailExportVo r:list
        ) {
            r.setCustPattern("0");
        }
        try {
            ExcelUtil<RetailExportVo> util = new ExcelUtil<>(RetailExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "我的客户-我的客户导出1000条（个人）")
    @PostMapping("/exportRetail1000")
    @ApiOperation("我的客户导出1000条（个人）")
    public void exportRetail1000(HttpServletResponse response, @RequestBody CustBaseInfo sysCustomerBasedata)
    {
        sysCustomerBasedata.setSize(1000);
        sysCustomerBasedata.setNum(1);
        List<CustBaseInfo> custBaseInfoList = myCustomerService.selectSysCustomerBasedataList(sysCustomerBasedata);
        List<RetailExportVo> list = new ArrayList<>();
        for (CustBaseInfo b:custBaseInfoList
        ) {
            RetailExportVo retailExportVo = new RetailExportVo();
            BeanUtils.copyProperties(b,retailExportVo);
            retailExportVo.setCustPattern("0");
            list.add(retailExportVo);
        }
        try {
            ExcelUtil<RetailExportVo> util = new ExcelUtil<>(RetailExportVo.class);
            util.exportExcel(response, list, "sheet1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Log(title = "我的客户-补充经纬度")
    @PostMapping("/selectAddress")
    @ApiOperation("补充经纬度")
    public AjaxResult selectAddress(@RequestBody List<CustBaseInfo> list)
    {
        return AjaxResult.success(myCustomerService.selectAddress(list));
    }


    @Log(title = "我的客户-添加至已有活动")
    @PostMapping("/appointCustCamp")
    @ApiOperation("添加至已有活动")
    public AjaxResult appointCustCamp(String custId,String custName,String custIdc,String custPhone,String custIsn,String socialCreditCode,String lpName,String campaignId,String custType) throws ServerException {
        if(!StringUtils.isNotEmpty(custId)){
            return AjaxResult.warn("客户ID不能为空");
        }
        return AjaxResult.success(iSysCampaignGroupCustomerService.appointCustCamp( custId, custName, custIdc, custPhone, custIsn,socialCreditCode,lpName, campaignId, custType));
    }

    @Log(title = "我的客户-异步导入企业客户价值分层", businessType = com.ruoyi.common.enums.BusinessType.IMPORT)
    @PostMapping(value = "/importBusinessCustLevelAsync", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("异步导入企业客户价值分层")
    public AjaxResult importBusinessCustLevelAsync(@RequestPart("file") MultipartFile file) {
        return AjaxResult.success("导入任务已提交，后台正在处理", myCustomerService.importBusinessCustLevelAsync(file));
    }

    @Log(title = "我的客户-查询企业客户价值分层导入状态")
    @GetMapping("/importBusinessCustLevelStatus/{taskId}")
    @ApiOperation("查询企业客户价值分层导入状态")
    public AjaxResult importBusinessCustLevelStatus(@PathVariable String taskId) {
        return AjaxResult.success(myCustomerService.getBusinessCustLevelImportStatus(taskId));
    }
}
