package com.fs.qw.controller;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.utils.ServletUtils;
import com.fs.course.config.CourseConfig;
import com.fs.framework.web.service.TokenService;
import com.fs.qw.vo.QwOptionsVO;
import com.fs.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.QwCompany;
import com.fs.qw.service.IQwCompanyService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 企微主体Controller
 *
 * @author fs
 * @date 2024-10-09
 */
@RestController
@RequestMapping("/qw/qwCompany")
public class QwCompanyController extends BaseController
{
    @Autowired
    private IQwCompanyService qwCompanyService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询企微主体列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwCompany qwCompany)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        if(!loginUser.isAdmin() && config.getIsBound() != null && config.getIsBound()){
            qwCompany.setCreateDeptId(loginUser.getDeptId());
            qwCompany.setCreateUserId(loginUser.getUserId());
        }
        List<QwCompany> list = qwCompanyService.selectQwCompanyList(qwCompany);
        return getDataTable(list);
    }


    @GetMapping("/all")
    public R all()
    {
        Long userId = null;
        Long deptId = null;
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        if(!loginUser.isAdmin() && config.getIsBound() != null && config.getIsBound()){
            deptId = loginUser.getDeptId();
            if(config.getDept() == null || !config.getDept()){
                userId = loginUser.getUserId();
            }
        }
        List<QwOptionsVO> list = qwCompanyService.selectQwCompanyListOptionsVO(userId, deptId);
        return R.ok().put("data", list);
    }
    /**
     * 导出企微主体列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:export')")
    @Log(title = "企微主体", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwCompany qwCompany)
    {
        List<QwCompany> list = qwCompanyService.selectQwCompanyList(qwCompany);
        ExcelUtil<QwCompany> util = new ExcelUtil<QwCompany>(QwCompany.class);
        return util.exportExcel(list, "企微主体数据");
    }

    /**
     * 获取企微主体详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwCompanyService.selectQwCompanyById(id));
    }

    /**
     * 新增企微主体
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:add')")
    @Log(title = "企微主体", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwCompany qwCompany)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        qwCompany.setCreateUserId(loginUser.getUserId());
        qwCompany.setCreateDeptId(loginUser.getDeptId());
        return toAjax(qwCompanyService.insertQwCompany(qwCompany));
    }

    /**
     * 修改企微主体
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:edit')")
    @Log(title = "企微主体", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwCompany qwCompany)
    {
        return toAjax(qwCompanyService.updateQwCompany(qwCompany));
    }

    /**
     * 删除企微主体
     */
    @PreAuthorize("@ss.hasPermi('qw:qwCompany:remove')")
    @Log(title = "企微主体", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwCompanyService.deleteQwCompanyByIds(ids));
    }
}
