package com.fs.qw.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.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.qw.domain.QwDept;
import com.fs.qw.mapper.QwCompanyMapper;
import com.fs.qw.service.IQwDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业微信部门Controller
 *
 * @author fs
 * @date 2024-08-27
 */
@RestController
@RequestMapping("/qw/qwDept")
public class QwDeptController extends BaseController
{
    @Autowired
    private IQwDeptService qwDeptService;


    /**
     * 查询企业微信部门列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwDept qwDept)
    {
        startPage();
        List<QwDept> list = qwDeptService.selectQwDeptList(qwDept);
        return getDataTable(list);
    }
    @Autowired
    QwCompanyMapper qwCompanyMapper;

    /**
     * 同步企业微信 部门信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:list')")
    @GetMapping("/syncDept/{companyId}")
    public R syncDept(@PathVariable("companyId") Long companyId){

        List<String> strings = qwCompanyMapper.selectQwCompanyCorpIdListByCompanyId(companyId);
        for (String string : strings) {
            qwDeptService.insertOrUpdateQwDept(string);
        }

        return  R.ok();
    }

    /**
     * 导出企业微信部门列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:export')")
    @Log(title = "企业微信部门", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwDept qwDept)
    {
        List<QwDept> list = qwDeptService.selectQwDeptList(qwDept);
        ExcelUtil<QwDept> util = new ExcelUtil<QwDept>(QwDept.class);
        return util.exportExcel(list, "企业微信部门数据");
    }

    /**
     * 获取企业微信部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwDeptService.selectQwDeptById(id));
    }

    /**
     * 新增企业微信部门
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:add')")
    @Log(title = "企业微信部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwDept qwDept)
    {
        return toAjax(qwDeptService.insertQwDept(qwDept));
    }

    /**
     * 修改企业微信部门
     */
    @PreAuthorize("@ss.hasPermi('qw:qwDept:edit')")
    @Log(title = "企业微信部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwDept qwDept)
    {
        return toAjax(qwDeptService.updateQwDept(qwDept));
    }

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

    /**
     * @Description: 获取企微部门 按Treeselect返回 每一个企微主体有自己的部门，按企微主体查询
     * @Param:
     * @Return:
     * @Author xgb
     * @Date 2025/10/30 9:33
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(QwDept qwDept)
    {
        if(StringUtils.isEmpty(qwDept.getCorpId())){
            return AjaxResult.error("请选择企微主体");
        }
        List<QwDept> depts = qwDeptService.selectQwDeptList(qwDept);
        return AjaxResult.success(qwDeptService.buildDeptTreeSelect(depts));
    }

}
