package com.ruoyi.mall.controller;

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

import com.alibaba.fastjson.JSONObject;
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.MallAppCompanyUser;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IMallShopShopService;
import com.ruoyi.system.service.ISysUserService;
import netscape.javascript.JSObject;
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.MallSalesman;
import com.ruoyi.mall.service.IMallSalesmanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 业务员Controller
 *
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/mall/mallSalesman")
public class MallSalesmanController extends BaseController
{
    @Autowired
    private IMallSalesmanService mallSalesmanService;
    @Autowired
    private IMallMemberUserService mallMemberUserService;
    @Autowired
    private IMallShopShopService shopShopService;
    @Resource
    private ISysUserService userService;
    /**
     * 后台重置业务员密码
     * @param param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:edit')")
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody Map<String,Object> param){
        if (!param.containsKey("salesmanId") || !param.containsKey("newPassword")){
            return AjaxResult.error("缺少参数");
        }
        try {

            return mallSalesmanService.resetPassword(param);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("网络异常");
        }
    }

    /**
     * 查询业务员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallSalesman mallSalesman)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallSalesman.setShopId(shop.getShopId());
        }

        startPage();
        List<MallSalesman> list = mallSalesmanService.selectMallSalesmanList(mallSalesman);
        return getDataTable(list);
    }

    /**
     * 查询业务员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:list')")
    @GetMapping("/selectList")
    public AjaxResult selectList()
    {

        MallSalesman mallSalesman = new MallSalesman();
        // 是否店铺权限
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallSalesman.setShopId(shop.getShopId());
        }
        List<MallSalesman> list = mallSalesmanService.selectMallSalesmanList(mallSalesman);
        return AjaxResult.success(list);
    }

    /**
     * 导出业务员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:export')")
    @Log(title = "业务员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallSalesman mallSalesman)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallShopShop shop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(shop)){
            mallSalesman.setShopId(shop.getShopId());
        }
        List<MallSalesman> list = mallSalesmanService.selectMallSalesmanList(mallSalesman);
        ExcelUtil<MallSalesman> util = new ExcelUtil<MallSalesman>(MallSalesman.class);
        return util.exportExcel(list, "业务员数据");
    }

    /**
     * 获取业务员详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mallSalesmanService.selectMallSalesmanById(id));
    }

    /**
     * 新增业务员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:add')")
    @Log(title = "业务员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallSalesman mallSalesman)
    {

        MallSalesman mallSalesman1 = new MallSalesman();
        mallSalesman1.setPhone(mallSalesman.getPhone());
        List<MallSalesman> mallSalesmen = mallSalesmanService.selectMallSalesmanList(mallSalesman);
        if (mallSalesmen.size() > 0){
            return AjaxResult.error("手机号已被他人使用");
        }
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(mallSalesman.getPhone())))
        {
            return AjaxResult.error("新增人员'" + mallSalesman.getPhone() + "'失败，登录账号已存在");
        }

        if (mallSalesman.getCompanyUserType() == 0) {
            MallSalesman salesman = new MallSalesman();
            salesman.setCompanyId(mallSalesman.getCompanyId());
            salesman.setRoleId(10L);
            List<MallSalesman> salesmanList = mallSalesmanService.selectMallSalesmanList(salesman);
            if (salesmanList.size() > 0) {
                return AjaxResult.error("分公司管理员最多创建1个");
            }
        }
        return toAjax(mallSalesmanService.insertMallSalesman(mallSalesman));
    }

    /**
     * 修改业务员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:edit')")
    @Log(title = "业务员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallSalesman mallSalesman)
    {
        MallSalesman mallSalesman1 = new MallSalesman();
        mallSalesman1.setPhone(mallSalesman.getPhone());
        List<MallSalesman> mallSalesmen = mallSalesmanService.selectMallSalesmanList(mallSalesman);
        if (mallSalesmen.size() > 0){
            for (MallSalesman salesman : mallSalesmen) {
                if (!salesman.getPhone().equals(mallSalesman.getPhone())){
                    return AjaxResult.error("手机号已被他人使用");
                }
            }
        }

        if (mallSalesman.getCompanyUserType() == 0) {
            MallSalesman salesman = new MallSalesman();
            salesman.setCompanyId(mallSalesman.getCompanyId());
            salesman.setRoleId(10L);
            List<MallSalesman> salesmanList = mallSalesmanService.selectMallSalesmanList(salesman);
            if (salesmanList.size() > 0 && !salesmanList.get(0).getId().equals(mallSalesman.getId())) {
                return AjaxResult.error("分公司管理员最多创建1个");
            }
        }
        return toAjax(mallSalesmanService.updateMallSalesman(mallSalesman));
    }

    /**
     * 删除业务员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSalesman:remove')")
    @Log(title = "业务员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mallSalesmanService.deleteMallSalesmanByIds(ids));
    }

    /**
     * 查询业务员列表
     */
    @GetMapping("/selectMemberList")
    public AjaxResult selectMemberList()
    {
        MallMemberUser mallMemberUser = new MallMemberUser();
        mallMemberUser.setDelFlag("0");
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        return AjaxResult.success(list);
    }

    /**
     * 查看业务员二维码
     * @param mallSalesman 业务员
     * @return 结果
     */
    @PostMapping("/getSalesmanQrCode")
    public AjaxResult getSalesmanQrCode(@RequestBody MallSalesman mallSalesman){
        JSONObject salesmanQrCode = mallSalesmanService.getSalesmanQrCode(mallSalesman);
        return AjaxResult.success(salesmanQrCode);
    }
}
