package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.ObjectUtils;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallAppCompanyUser;
import com.ruoyi.mall.service.IMallAppCompanyUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * app-公司人员Controller
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/mall/mallAppCompanyUser")
public class MallAppCompanyUserController extends BaseController
{
    @Resource
    private IMallAppCompanyUserService mallAppCompanyUserService;
    @Resource
    private ISysUserService userService;

    /**
     * 查询app-公司人员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallAppCompanyUser mallAppCompanyUser)
    {
        startPage();
        mallAppCompanyUser.setDelFlag(0);
        List<MallAppCompanyUser> list = mallAppCompanyUserService.selectMallAppCompanyUserList(mallAppCompanyUser);
        return getDataTable(list);
    }

    /**
     * 导出app-公司人员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:export')")
    @Log(title = "app-公司人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallAppCompanyUser mallAppCompanyUser)
    {
        List<MallAppCompanyUser> list = mallAppCompanyUserService.selectMallAppCompanyUserList(mallAppCompanyUser);
        ExcelUtil<MallAppCompanyUser> util = new ExcelUtil<MallAppCompanyUser>(MallAppCompanyUser.class);
        return util.exportExcel(list, "app-公司人员数据");
    }

    /**
     * 获取app-公司人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:query')")
    @GetMapping(value = "/{companyUserId}")
    public AjaxResult getInfo(@PathVariable("companyUserId") Long companyUserId)
    {
        return AjaxResult.success(mallAppCompanyUserService.selectMallAppCompanyUserByCompanyUserId(companyUserId));
    }

    /**
     * 新增app-公司人员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:add')")
    @Log(title = "app-公司人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallAppCompanyUser mallAppCompanyUser)
    {
        MallAppCompanyUser companyUserName = mallAppCompanyUserService.selectMallAppCompanyUserByCompanyUserName(mallAppCompanyUser.getCompanyUserName());
        if (companyUserName != null) {
            return AjaxResult.error("人员名称已存在");
        }

        MallAppCompanyUser companyUserAccount = mallAppCompanyUserService.selectMallAppCompanyUserByCompanyUserAccount(mallAppCompanyUser.getCompanyUserAccount());
        if (companyUserAccount != null) {
            return AjaxResult.error("人员账号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallAppCompanyUser.getCompanyUserAccount())))
        {
            return AjaxResult.error("新增人员'" + mallAppCompanyUser.getCompanyUserAccount() + "'失败，登录账号已存在");
        }

        if (mallAppCompanyUser.getCompanyUserType() == 0) {
            MallAppCompanyUser user = new MallAppCompanyUser();
            user.setCompanyId(mallAppCompanyUser.getCompanyId());
            user.setRoleId(10L);
            List<MallAppCompanyUser> companyUserList = mallAppCompanyUserService.selectMallAppCompanyUserList(user);
            if (companyUserList.size() > 0) {
                return AjaxResult.error("仓库管理员最多创建1个");
            }
        }
        return toAjax(mallAppCompanyUserService.insertMallAppCompanyUser(mallAppCompanyUser));
    }

    /**
     * 修改app-公司人员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:edit')")
    @Log(title = "app-公司人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallAppCompanyUser mallAppCompanyUser)
    {
        MallAppCompanyUser companyUserName = mallAppCompanyUserService.selectMallAppCompanyUserByCompanyUserName(mallAppCompanyUser.getCompanyUserName());
        if (companyUserName != null && !companyUserName.getCompanyUserId().equals(mallAppCompanyUser.getCompanyUserId())) {
            return AjaxResult.error("人员名称已存在");
        }

        MallAppCompanyUser companyUserAccount = mallAppCompanyUserService.selectMallAppCompanyUserByCompanyUserAccount(mallAppCompanyUser.getCompanyUserAccount());
        if (companyUserAccount != null && !companyUserAccount.getCompanyUserId().equals(mallAppCompanyUser.getCompanyUserId())) {
            return AjaxResult.error("人员账号已存在");
        }

        MallAppCompanyUser sysUserId = mallAppCompanyUserService.selectMallAppCompanyUserBySysUserId(mallAppCompanyUser.getSysUserId());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallAppCompanyUser.getCompanyUserAccount())) && !sysUserId.getSysUserId().equals(mallAppCompanyUser.getSysUserId()))
        {
            return AjaxResult.error("新增人员'" + mallAppCompanyUser.getCompanyUserAccount() + "'失败，登录账号已存在");
        }

        if (mallAppCompanyUser.getCompanyUserType() == 0) {
            MallAppCompanyUser user = new MallAppCompanyUser();
            user.setCompanyId(mallAppCompanyUser.getCompanyId());
            user.setRoleId(10L);
            List<MallAppCompanyUser> companyUserList = mallAppCompanyUserService.selectMallAppCompanyUserList(user);
            if (companyUserList.size() > 0 && !companyUserList.get(0).getCompanyUserId().equals(mallAppCompanyUser.getCompanyUserId())) {
                return AjaxResult.error("仓库管理员最多创建1个");
            }
        }
        return toAjax(mallAppCompanyUserService.updateMallAppCompanyUser(mallAppCompanyUser));
    }

    /**
     * 删除app-公司人员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallAppCompanyUser:remove')")
    @Log(title = "app-公司人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyUserIds}")
    public AjaxResult remove(@PathVariable Long[] companyUserIds)
    {
        return toAjax(mallAppCompanyUserService.deleteMallAppCompanyUserByCompanyUserIds(companyUserIds));
    }

    /**
     * 查询业务员列表
     */
    @GetMapping("/selectAppSalesmanList")
    public AjaxResult selectAppSalesmanList()
    {
        MallAppCompanyUser companyUserName = new MallAppCompanyUser();
        companyUserName.setCompanyUserType(1);
        // 权限
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallAppCompanyUser mallAppCompanyUser = mallAppCompanyUserService.selectMallAppCompanyUserBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(mallAppCompanyUser)) {
            if (mallAppCompanyUser.getRoleId() == 10) {
                companyUserName.setCompanyUserId(mallAppCompanyUser.getCompanyUserId());
            }
            if (mallAppCompanyUser.getRoleId() == 11) {
                companyUserName.setSysUserId(loginUser.getUserId());
            }
        }
        List<MallAppCompanyUser> list = mallAppCompanyUserService.selectMallSalesmanList(companyUserName);
        return AjaxResult.success(list);
    }
}
