package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.constant.UserConstants;
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.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.vo.CompanyUserOptionRespVO;
import org.apache.http.util.Asserts;
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 2021-10-04
 */
@RestController
@RequestMapping("/company/companyUser")
public class CompanyUserController extends BaseController {
    @Autowired
    private ICompanyUserService companyUserService;

    /**
     * 查询企业员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyUser companyUser) {
        startPage();
        List<CompanyUser> list = companyUserService.selectCompanyUserList(companyUser);
        return getDataTable(list);
    }

    /**
     * 导出企业员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:export')")
    @Log(title = "企业员工信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyUser companyUser) {
        List<CompanyUser> list = companyUserService.selectCompanyUserList(companyUser);
        ExcelUtil<CompanyUser> util = new ExcelUtil<CompanyUser>(CompanyUser.class);
        return util.exportExcel(list, "companyUser");
    }

    /**
     * 获取企业员工信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(companyUserService.selectCompanyUserById(userId));
    }

    /**
     * 新增企业员工信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:add')")
    @Log(title = "企业员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyUser companyUser) {
        if (UserConstants.NOT_UNIQUE.equals(companyUserService.checkUserNameUnique(companyUser.getUserName()))) {
            return AjaxResult.error("新增用户'" + companyUser.getUserName() + "'失败，登录账号已存在");
        }
        return toAjax(companyUserService.insertCompanyUser(companyUser));
    }

    /**
     * 修改企业员工信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:edit')")
    @Log(title = "企业员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyUser companyUser) {
        return toAjax(companyUserService.updateCompanyUser(companyUser));
    }

    /**
     * 删除企业员工信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:remove')")
    @Log(title = "企业员工信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(companyUserService.deleteCompanyUserByIds(userIds));
    }

    @GetMapping("/getAllUserlist")
    public R getAllUserlist(@RequestParam Long companyId) {
        CompanyUser map = new CompanyUser();
        map.setCompanyId(companyId);
        List<CompanyUser> list = companyUserService.selectCompanyUserList(map);
        return R.ok().put("data", list);
    }


    @GetMapping("/getUserOptionVOList")
    public R getCompanyUserOptionVOList(@RequestParam(value = "companyNickName") String companyNickName,
                                        @RequestParam(required = false,value = "companyId") Long companyId) {
        List<CompanyUserOptionRespVO> list = companyUserService.getUserOptionVOList(companyNickName,companyId);
        return R.ok().put("data", list);
    }


    /**
     * 获取业务员的产品
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:query')")
    @GetMapping(value = "getCompanyUserProductList")
    public AjaxResult getCompanyUserProductList(@RequestParam("id") Long id) {
        return AjaxResult.success(companyUserService.getCompanyUserProductList(id));
    }

    /**
     * 查询当前公司的所有客服
     * @param companyId 公司ID
     * @return 客服列表
     */
    @GetMapping("/queryAllCompanyUser")
    public R queryAllCompanyUser(@RequestParam("companyId") Long companyId) {
        Asserts.notNull(companyId,"公司ID不能为空！");

        List<CompanyUser> companyUsers = companyUserService.queryAllCompanyUser(companyId);
        return R.ok().put("data", companyUsers);
    }


}
