package com.fs.company.controller;

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.domain.R;
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.CompanyWxClient;
import com.fs.company.service.ICompanyVoiceRoboticService;
import com.fs.company.service.ICompanyWxClientService;
import com.fs.company.vo.AddWxClientVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 添加个微信账号Controller
 * 
 * @author fs
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/company/companyClient")
public class CompanyWxClientController extends BaseController
{
    @Autowired
    private ICompanyWxClientService companyWxClientService;
    @Autowired
    private ICompanyVoiceRoboticService companyVoiceRoboticService;

    /**
     * 查询添加个微信账号列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyWxClient companyWxClient){
        startPage();
        List<CompanyWxClient> list = companyWxClientService.selectCompanyWxClientList(companyWxClient);
        return getDataTable(list);
    }

    /**
     * 导出添加个微信账号列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:export')")
    @Log(title = "添加个微信账号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyWxClient companyWxClient)
    {
        List<CompanyWxClient> list = companyWxClientService.selectCompanyWxClientList(companyWxClient);
        ExcelUtil<CompanyWxClient> util = new ExcelUtil<CompanyWxClient>(CompanyWxClient.class);
        return util.exportExcel(list, "companyClient");
    }

    /**
     * 获取添加个微信账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyWxClientService.selectCompanyWxClientById(id));
    }

    /**
     * 新增添加个微信账号
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:add')")
    @Log(title = "添加个微信账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyWxClient companyWxClient)
    {
        return toAjax(companyWxClientService.insertCompanyWxClient(companyWxClient));
    }

    /**
     * 修改添加个微信账号
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:edit')")
    @Log(title = "添加个微信账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyWxClient companyWxClient)
    {
        return toAjax(companyWxClientService.updateCompanyWxClient(companyWxClient));
    }

    /**
     * 删除添加个微信账号
     */
    @PreAuthorize("@ss.hasPermi('company:companyClient:remove')")
    @Log(title = "添加个微信账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(companyWxClientService.deleteCompanyWxClientByIds(ids));
    }


    @PostMapping("/addWxClient")
    @Transactional
    public R addWxClient(@RequestBody AddWxClientVo vo){
        companyVoiceRoboticService.addCompany(vo);
        return R.ok();
    }
    @GetMapping("/addWxStatistics")
    @Transactional
    public R addWxStatistics(CompanyWxClient companyWxClient){
        List<CompanyWxClient> clients = companyWxClientService.selectCompanyWxClientList(companyWxClient);
        LocalDate now = LocalDate.now();
        return R.ok()
                .put("totalNum", clients.size())
                .put("toDayAddNum", clients.stream().filter(e -> e.getIsAdd() == 1 && e.getAddTime() != null && e.getAddTime().toLocalDate().equals(now)).count());
    }

    @GetMapping("/toDayAddWxAccountList")
    @Transactional
    public R toDayAddWxAccountList(Long companyId, Long accountId){
        return R.ok().put("data", companyWxClientService.toDayAddWxAccountList(companyId, accountId, 0));
    }
}
