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.his.vo.OptionsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业员工信息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");
    }

    /**
     * 获取所有的销售列表
     * @param companyId
     * @return
     */
    @GetMapping("/getUserList")
    public R getUserList(@RequestParam("companyId") Long companyId)
    {
        CompanyUser map=new CompanyUser();
        map.setCompanyId(companyId);
        List<CompanyUser> list = companyUserService.selectCompanyUserList(map);
        return  R.ok().put("data",list);
    }
    /**
     * 获取企业员工信息详细信息
     */
    @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("/getAllUserListLimit")
    public R getAllUserListLimit(@RequestParam(required = false) Long companyId,
                                 @RequestParam(required = false) String keywords){
        List<CompanyUser> list = companyUserService.getAllUserListLimit(companyId,keywords);
        return R.ok().put("data", list);
    }

    @GetMapping("/getUserListByDeptId")
    public R getUserListByDeptId(CompanyUser user)
    {

        List<CompanyUser> list = companyUserService.getUserListByDeptId(user);
        return R.ok().put("data",list);
    }

    /**
     * 根据登录的用户公司获取所有的销售
     * @return
     */
    @GetMapping("/getCompanyUserList")
    public R getCompanyUserList(@RequestParam Long companyId)
    {
        CompanyUser cu = new CompanyUser();
        cu.setCompanyId(companyId);
        List<CompanyUser> list = companyUserService.selectCompanyUserList(cu);
        return  R.ok().put("data",list);
    }

    /**
     * 更换会员归属销售
     * @return
     */
    @PreAuthorize("@ss.hasPermi('company:companyUser:change')")
    @Log(title = "更换会员归属", businessType = BusinessType.OTHER)
    @PostMapping("/changeCompanyUser")
    public AjaxResult changeCompanyUser(@RequestBody List<Long> userIds, @RequestParam Long companyUserId, @RequestParam Long companyId)
    {
        return toAjax(companyUserService.changeCompanyUser(userIds, companyUserId, companyId));
    }


    @GetMapping("/getCompanyUserListLikeName")
    public R getCompanyUserListLikeName(@RequestParam(required = false) String name,
                                        @RequestParam(required = false) Long companyId,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String,Object> params = new HashMap<>();
        params.put("companyUserName", name);
        params.put("companyId", companyId);

        PageHelper.startPage(pageNum, pageSize);
        List<OptionsVO> companyUserList = companyUserService.selectCompanyUserListByMap(params);
        return R.ok().put("data", new PageInfo<>(companyUserList));
    }
}
