package com.fs.qw.controller;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fs.common.exception.ServiceException;
import com.fs.qw.param.QwExternalContactParam;
import com.fs.qw.param.QwTagSearchParam;
import com.fs.qw.service.IQwTagService;
import com.fs.qw.vo.QwExternalContactUnionIdExportVO;
import com.fs.qw.vo.QwExternalContactVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 企业微信客户Controller
 * 
 * @author fs
 * @date 2025-06-13
 */
@RestController
@RequestMapping("/qw/externalContact")
public class QwExternalContactController extends BaseController
{
    private IQwExternalContactService qwExternalContactService;
    private IQwTagService iQwTagService;

    QwExternalContactController(IQwExternalContactService qwExternalContactService,IQwTagService iQwTagService){
        this.qwExternalContactService=qwExternalContactService;
        this.iQwTagService=iQwTagService;
    }


    /**
     * 查询企业微信客户列表
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwExternalContactParam qwExternalContact)
    {
        startPage();
        List<QwExternalContactVO> list = qwExternalContactService.selectQwExternalContactListVONewSys(qwExternalContact);
        list.forEach(item->{

            if (!Objects.equals(item.getTagIds(), "[]") && item.getTagIds()!=null) {
                QwTagSearchParam param = new QwTagSearchParam();
                Gson gson = new Gson();
                List<String> tagIds = gson.fromJson(
                        item.getTagIds(),
                        new TypeToken<List<String>>() {
                        }.getType()
                );

                param.setTagIds(tagIds);

                item.setTagIdsName(iQwTagService.selectQwTagListByTagIds(param));
            }
        });

        return getDataTable(list);
    }

    /**
     * 导出企业微信客户列表
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:export')")
    @Log(title = "企业微信客户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwExternalContactParam qwExternalContact)
    {
        if (qwExternalContact.getCompanyId() == null) {
            return AjaxResult.success();
        }

        List<QwExternalContactVO> list = qwExternalContactService.selectQwExternalContactListVO(qwExternalContact);
        list.forEach(item->{

            if (!Objects.equals(item.getTagIds(), "[]") && item.getTagIds()!=null) {
                QwTagSearchParam param = new QwTagSearchParam();
                Gson gson = new Gson();
                List<String> tagIds = gson.fromJson(
                        item.getTagIds(),
                        new TypeToken<List<String>>() {
                        }.getType()
                );

                param.setTagIds(tagIds);

                item.setTagIdsName(iQwTagService.selectQwTagListByTagIds(param));
            }
        });
        ExcelUtil<QwExternalContactVO> util = new ExcelUtil<QwExternalContactVO>(QwExternalContactVO.class);
        return util.exportExcel(list, "企业微信客户数据");
    }

    /**
     * 导出企业微信客户unionId
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:export')")
    @Log(title = "企业微信客户unionId", businessType = BusinessType.EXPORT)
    @GetMapping("/exportUnionId")
    public AjaxResult exportUnionId(QwExternalContactParam qwExternalContact)
    {
        List<QwExternalContactUnionIdExportVO> list = qwExternalContactService.selectQwExternalContactUnionIdExportVO(qwExternalContact);
        ExcelUtil<QwExternalContactUnionIdExportVO> util = new ExcelUtil<QwExternalContactUnionIdExportVO>(QwExternalContactUnionIdExportVO.class);
        return util.exportExcel(list, "企业微信客户unionId数据");
    }


    /**
     * 获取企业微信客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwExternalContactService.selectQwExternalContactById(id));
    }

    /**
     * 新增企业微信客户
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:add')")
    @Log(title = "企业微信客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwExternalContact qwExternalContact)
    {
        return toAjax(qwExternalContactService.insertQwExternalContact(qwExternalContact));
    }

    /**
     * 修改企业微信客户
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:edit')")
    @Log(title = "企业微信客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwExternalContact qwExternalContact)
    {
        return toAjax(qwExternalContactService.updateQwExternalContact(qwExternalContact));
    }

    /**
     * 删除企业微信客户
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:remove')")
    @Log(title = "企业微信客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwExternalContactService.deleteQwExternalContactByIds(ids));
    }
}
