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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.param.CompanyWxUserAddGroupParam;
import com.fs.wxUser.domain.CompanyWxUser;
import com.fs.wxUser.param.CompanyWxUserBindFsUserParam;
import com.fs.wxUser.param.CompanyWxUserParam;
import com.fs.wxUser.service.ICompanyWxUserService;
import com.fs.wxUser.vo.CompanyWxUserVO;
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-10-22
 */
@RestController
@RequestMapping("/company/wxUser")
public class CompanyWxUserController extends BaseController
{
    @Autowired
    private ICompanyWxUserService companyWxUserService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询个微用户列表
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyWxUserParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyWxUserVO> list = companyWxUserService.selectCompanyWxUserListVO(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('company:wxUser:list')")
    @GetMapping("/listAll")
    public R listAll(CompanyWxUserParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        return R.ok().put("data", companyWxUserService.selectCompanyWxUserListVO(param));
    }
    /**
     * 导出个微用户列表
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:export')")
    @Log(title = "个微用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyWxUserParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyWxUserVO> list = companyWxUserService.selectCompanyWxUserListVO(param);
        ExcelUtil<CompanyWxUserVO> util = new ExcelUtil<CompanyWxUserVO>(CompanyWxUserVO.class);
        return util.exportExcel(list, "个微用户数据");
    }

    /**
     * 获取个微用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(companyWxUserService.selectCompanyWxUserByUserId(userId));
    }

    /**
     * 新增个微用户
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:add')")
    @Log(title = "个微用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyWxUser companyWxUser)
    {
        return toAjax(companyWxUserService.insertCompanyWxUser(companyWxUser));
    }

    /**
     * 修改个微用户
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:edit')")
    @Log(title = "个微用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyWxUser companyWxUser)
    {
        return toAjax(companyWxUserService.updateCompanyWxUser(companyWxUser));
    }

    /**
     * 删除个微用户
     */
    @PreAuthorize("@ss.hasPermi('company:wxUser:remove')")
    @Log(title = "个微用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(companyWxUserService.deleteCompanyWxUserByUserIds(userIds));
    }

    /**
    *  绑定小程序id
    */
    @PutMapping("/bindMiniUserId")
    public R bindUserId(@RequestBody CompanyWxUserBindFsUserParam param)
    {
        return companyWxUserService.updateCompanyWxUserBindFsUser(param);
    }

    /**
    * 批量添加分组
    */

    @PreAuthorize("@ss.hasPermi('company:wxUser:addGroup')")
    @Log(title = "批量添加分组", businessType = BusinessType.INSERT)
    @PostMapping("/addGroup")
    public R addGroup(@RequestBody CompanyWxUserAddGroupParam param)
    {

        return companyWxUserService.addGroup(param);
    }

    /**
    * 批量移除分组
    */
    @PreAuthorize("@ss.hasPermi('company:wxUser:delGroup')")
    @Log(title = "批量移除分组", businessType = BusinessType.INSERT)
    @PostMapping("/delGroup")
    public R delGroup(@RequestBody CompanyWxUserAddGroupParam param)
    {

        return companyWxUserService.delGroup(param);
    }

}
