package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyVoiceRoboticCallLogAddwx;
import com.fs.company.domain.CompanyWxClient;
import com.fs.company.service.ICompanyVoiceRoboticCallLogAddwxService;
import com.fs.company.vo.CompanyVoiceRoboticCallLogAddWxExportVO;
import com.fs.company.vo.CompanyVoiceRoboticCallLogAddwxVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 调用日志_加微信Controller
 * 
 * @author fs
 * @date 2026-01-15
 */
@RestController
@RequestMapping("/company/addwxLog")
public class CompanyVoiceRoboticCallLogAddwxController extends BaseController
{
    @Autowired
    private ICompanyVoiceRoboticCallLogAddwxService companyVoiceRoboticCallLogAddwxService;

    /**
     * 查询调用日志_加微信列表
     */
    @PreAuthorize("@ss.hasPermi('company:addwxlog:list')")
    @GetMapping("/list")
    public TableDataInfo listByCustomerIdAndRoboticId(CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
    {
//        if( null == companyVoiceRoboticCallLogAddwx.getCustomerId() || null == companyVoiceRoboticCallLogAddwx.getRoboticId()){
//            return getDataTable(null);
//        }
        //查询用户
        if(null == companyVoiceRoboticCallLogAddwx.getRoboticId()){
            List<CompanyWxClient> wxclients = companyVoiceRoboticCallLogAddwxService.getWxClientInfoByCustomerId(companyVoiceRoboticCallLogAddwx.getCustomerId());
            if(null != wxclients && !wxclients.isEmpty()){
                Set<Long> collect = wxclients.stream().map(e -> e.getId()).collect(Collectors.toSet());
                companyVoiceRoboticCallLogAddwx.setWxclientIds(new ArrayList<>( collect));
                startPage();
                List<CompanyVoiceRoboticCallLogAddwxVO> companyVoiceRoboticCallLogAddwxVOS = companyVoiceRoboticCallLogAddwxService.listByCustomerId(companyVoiceRoboticCallLogAddwx);
                return getDataTable(companyVoiceRoboticCallLogAddwxVOS);
            } else{
                return getDataTable(Collections.EMPTY_LIST);
            }
        }
        else {
            CompanyWxClient wxClient = companyVoiceRoboticCallLogAddwxService.getWxClientInfoByCustomerIdAndRoboticId(companyVoiceRoboticCallLogAddwx.getCustomerId(), companyVoiceRoboticCallLogAddwx.getRoboticId());
            companyVoiceRoboticCallLogAddwx.setWxClientId(wxClient.getId());
            startPage();
            List<CompanyVoiceRoboticCallLogAddwxVO> list = companyVoiceRoboticCallLogAddwxService.listByCustomerIdAndRoboticId(companyVoiceRoboticCallLogAddwx);
            return getDataTable(list);
        }

    }

    /**
     * 查询调用日志_加微信列表
     */
    @PreAuthorize("@ss.hasPermi('company:addwxlog:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
    {
        startPage();
        List<CompanyVoiceRoboticCallLogAddwxVO> list = companyVoiceRoboticCallLogAddwxService.listAll(companyVoiceRoboticCallLogAddwx);
        TableDataInfo dataTable = getDataTable(list);
        Map<String, Long> countMap = companyVoiceRoboticCallLogAddwxService.countListAll(companyVoiceRoboticCallLogAddwx);
        if (countMap != null) {
            dataTable.setTotalRecordCount(countMap.getOrDefault("totalRecordCount", 0L));
            dataTable.setSuccessRecordCount(countMap.getOrDefault("successRecordCount", 0L));
            dataTable.setTodayRecordCount(countMap.getOrDefault("todayRecordCount", 0L));
        }
        return dataTable;

    }


    /**
     * 导出调用日志_加微信列表
     */
    @PreAuthorize("@ss.hasPermi('company:addwxlog:export')")
    @Log(title = "调用日志_加微信", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
    {
        List<CompanyVoiceRoboticCallLogAddwxVO> list = companyVoiceRoboticCallLogAddwxService.listAll(companyVoiceRoboticCallLogAddwx);
        List<CompanyVoiceRoboticCallLogAddWxExportVO> exportList = list.stream().map(item -> {
            CompanyVoiceRoboticCallLogAddWxExportVO vo = new CompanyVoiceRoboticCallLogAddWxExportVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<CompanyVoiceRoboticCallLogAddWxExportVO> util = new ExcelUtil<CompanyVoiceRoboticCallLogAddWxExportVO>(CompanyVoiceRoboticCallLogAddWxExportVO.class);
        return util.exportExcel(exportList, "调用日志_加微信数据");
    }

    /**
     * 删除调用日志_加微信
     */
    @PreAuthorize("@ss.hasPermi('company:addwxlog:remove')")
    @Log(title = "调用日志_加微信", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(companyVoiceRoboticCallLogAddwxService.deleteCompanyVoiceRoboticCallLogAddwxByLogIds(logIds));
    }

//    /**
//     * 导出调用日志_加微信列表
//     */
//    @PreAuthorize("@ss.hasPermi('company:addwxlog:export')")
//    @Log(title = "调用日志_加微信", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
//    {
//        List<CompanyVoiceRoboticCallLogAddwx> list = companyVoiceRoboticCallLogAddwxService.selectCompanyVoiceRoboticCallLogAddwxList(companyVoiceRoboticCallLogAddwx);
//        ExcelUtil<CompanyVoiceRoboticCallLogAddwx> util = new ExcelUtil<CompanyVoiceRoboticCallLogAddwx>(CompanyVoiceRoboticCallLogAddwx.class);
//        return util.exportExcel(list, "调用日志_加微信数据");
//    }
//

//
//    /**
//     * 获取调用日志_加微信详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('company:addwxlog:query')")
//    @GetMapping(value = "/{logId}")
//    public AjaxResult getInfo(@PathVariable("logId") Long logId)
//    {
//        return AjaxResult.success(companyVoiceRoboticCallLogAddwxService.selectCompanyVoiceRoboticCallLogAddwxByLogId(logId));
//    }
//
//    /**
//     * 新增调用日志_加微信
//     */
//    @PreAuthorize("@ss.hasPermi('company:addwxlog:add')")
//    @Log(title = "调用日志_加微信", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
//    {
//        return toAjax(companyVoiceRoboticCallLogAddwxService.insertCompanyVoiceRoboticCallLogAddwx(companyVoiceRoboticCallLogAddwx));
//    }
//
//    /**
//     * 修改调用日志_加微信
//     */
//    @PreAuthorize("@ss.hasPermi('company:addwxlog:edit')")
//    @Log(title = "调用日志_加微信", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CompanyVoiceRoboticCallLogAddwx companyVoiceRoboticCallLogAddwx)
//    {
//        return toAjax(companyVoiceRoboticCallLogAddwxService.updateCompanyVoiceRoboticCallLogAddwx(companyVoiceRoboticCallLogAddwx));
//    }
//
//    /**
//     * 删除调用日志_加微信
//     */
//    @PreAuthorize("@ss.hasPermi('company:addwxlog:remove')")
//    @Log(title = "调用日志_加微信", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{logIds}")
//    public AjaxResult remove(@PathVariable Long[] logIds)
//    {
//        return toAjax(companyVoiceRoboticCallLogAddwxService.deleteCompanyVoiceRoboticCallLogAddwxByLogIds(logIds));
//    }
}
