package com.ruoyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.cs.domain.CsOrderMemberUser;
import com.ruoyi.cs.domain.TeamUserVO;
import com.ruoyi.cs.domain.vo.CsOrderMenUserVo;
import com.ruoyi.cs.domain.vo.CsOrderUserVo;
import com.ruoyi.cs.mapper.CsOrderMemberUserMapper;
import com.ruoyi.cs.service.ICsOrderMemberUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工单用户信息Controller
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/cs/csOrderMemberUser")
public class CsOrderMemberUserController extends BaseController
{
    @Autowired
    private ICsOrderMemberUserService csOrderMemberUserService;

    @Resource
    private CsOrderMemberUserMapper csOrderMemberUserMapper;

    /**
     * 查询工单用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsOrderMemberUser csOrderMemberUser)
    {
        startPage();
        csOrderMemberUser.setDelFlag("0");
        List<CsOrderMemberUser> list = csOrderMemberUserService.selectCsOrderMemberUserList(csOrderMemberUser);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(l -> {
                if (StringUtils.isBlank(l.getUserAccount())){
                    l.setUserAccount(l.getEmail());
                }
                if (StringUtils.isBlank(l.getServiceStatus())){
                    l.setServiceStatus("1");
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出工单用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:export')")
    @Log(title = "工单用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsOrderMemberUser csOrderMemberUser)
    {
        csOrderMemberUser.setDelFlag("0");
        List<CsOrderMemberUser> list = csOrderMemberUserService.selectCsOrderMemberUserList(csOrderMemberUser);
        // List<CsOrderMemberUser> list = csOrderMemberUserService.selectCsUserInfoList(csOrderMemberUser);
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(l -> {
                if (StringUtils.isBlank(l.getUserAccount())){
                    l.setUserAccount(l.getEmail());
                }
                if (StringUtils.isBlank(l.getServiceStatus())){
                    l.setServiceStatus("1");
                }
            });
        }
        if (StringUtils.equals(csOrderMemberUser.getUserType(),"0")){
            ExcelUtil<CsOrderMemberUser> util = new ExcelUtil<CsOrderMemberUser>(CsOrderMemberUser.class);
            util.exportExcel(response, list, "工单用户信息数据");
        }
        if (StringUtils.equals(csOrderMemberUser.getUserType(),"1")){
            List<CsOrderMenUserVo> csOrderMenUserVos = BeanUtil.copyToList(list, CsOrderMenUserVo.class);
            ExcelUtil<CsOrderMenUserVo> util = new ExcelUtil<CsOrderMenUserVo>(CsOrderMenUserVo.class);
            util.exportExcel(response, csOrderMenUserVos, "资源用户信息数据");
        }
        if (StringUtils.equals(csOrderMemberUser.getUserType(),"2")){
            List<CsOrderUserVo> csOrderUserVos = BeanUtil.copyToList(list, CsOrderUserVo.class);
            ExcelUtil<CsOrderUserVo> util = new ExcelUtil<CsOrderUserVo>(CsOrderUserVo.class);
            util.exportExcel(response, csOrderUserVos, "特殊资源用户信息数据");
        }




    }

    /**
     * 获取工单用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:query')")
    @GetMapping(value = "/{memberUserId}")
    public AjaxResult getInfo(@PathVariable("memberUserId") String memberUserId)
    {
        return AjaxResult.success(csOrderMemberUserService.selectCsUserInfoByMemberUserId(memberUserId));
    }

    /**
     * 新增工单用户信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:add')")
    @Log(title = "新增工单用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsOrderMemberUser csOrderMemberUser)
    {
        CsOrderMemberUser userInfo = csOrderMemberUserMapper.getMemberUserInfo(null, csOrderMemberUser.getEmail(), null);
        if (ObjectUtils.isEmpty(userInfo)){
            // 注册
            String memberUserId = IdUtils.fastSimpleUUID();
            csOrderMemberUser.setMemberUserId(memberUserId);
            csOrderMemberUser.setPassword(SecurityUtils.encryptPassword(csOrderMemberUser.getPassword()));
            csOrderMemberUser.setStatus("2");
            csOrderMemberUser.setUserStatus("1");
            csOrderMemberUser.setCreateAt(DateUtils.getNowDate());
            csOrderMemberUser.setDelFlag("0");
            csOrderMemberUser.setGuideStatus("0");

            csOrderMemberUser.setInviteCode(memberUserId);
            csOrderMemberUserMapper.insertCsUserInfo(csOrderMemberUser);

            // 发送激活账号邮件
            csOrderMemberUserService.sendActivationEmail(new JSONObject(), csOrderMemberUser, 1);
            return AjaxResult.success();
        }else {
            return AjaxResult.error("邮箱已被注册");
        }

    }

    /**
     * 修改工单用户信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:edit')")
    @Log(title = "工单用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsOrderMemberUser csOrderMemberUser)
    {
        return toAjax(csOrderMemberUserService.updateCsUserInfo(csOrderMemberUser));
    }

    /**
     * 删除工单用户信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:remove')")
    @Log(title = "工单用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberUserIds}")
    public AjaxResult remove(@PathVariable String[] memberUserIds)
    {
        return toAjax(csOrderMemberUserService.deleteCsUserInfoByMemberUserIds(memberUserIds));
    }

    /**
     * 修改工单用户信息状态
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:edit')")
    @Log(title = "修改工单用户信息状态", businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody CsOrderMemberUser csOrderMemberUser)
    {
        return toAjax(csOrderMemberUserService.updateCsUserInfo(csOrderMemberUser));
    }

    /**
     * 查询工单用户、资源用户、特殊资源用户信息列表
     */
    @GetMapping("/selectAllUserListByUserType")
    public TableDataInfo selectAllUserListByUserType(CsOrderMemberUser csOrderMemberUser)
    {
        startPage();
        csOrderMemberUser.setDelFlag("0");
        List<CsOrderMemberUser> list = csOrderMemberUserService.selectAllUserListByUserType(csOrderMemberUser);
        return getDataTable(list);
    }

    /**
     * 团队信息列表
     */
    @GetMapping("/selectTeamUserList")
    public TableDataInfo selectTeamUserList(CsOrderMemberUser csOrderMemberUser)
    {
        startPage();
        csOrderMemberUser.setDelFlag("0");
        List<TeamUserVO> list = csOrderMemberUserService.selectTeamUserList(csOrderMemberUser);
        return getDataTable(list);
    }

    /**
     * 导出团队信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:export')")
    @Log(title = "团队信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTeamUserList")
    public void exportTeamUserList(HttpServletResponse response, CsOrderMemberUser csOrderMemberUser)
    {
        List<TeamUserVO> list = csOrderMemberUserService.selectTeamUserList(csOrderMemberUser);
        ExcelUtil<TeamUserVO> util = new ExcelUtil<TeamUserVO>(TeamUserVO.class);
        util.exportExcel(response, list, "团队信息列表");
    }

    /**
     * 获取团队详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:query')")
    @GetMapping(value = "/getTeamUserInfo")
    public AjaxResult getTeamUserInfo(CsOrderMemberUser csOrderMemberUser)
    {
        return AjaxResult.success(csOrderMemberUserService.selectTeamUserInfoByMemberUserId(csOrderMemberUser.getMemberUserId()));
    }

    /**
     * 用户重置密码
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:edit')")
    @Log(title = "用户重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody CsOrderMemberUser csOrderMemberUser)
    {
        return toAjax(csOrderMemberUserService.resetPassword(csOrderMemberUser));
    }

    /**
     * 团队成员列表
     */
    @GetMapping("/selectTeamMemberList")
    public TableDataInfo selectTeamMemberList(CsOrderMemberUser csOrderMemberUser)
    {
        startPage();
        csOrderMemberUser.setDelFlag("0");
        List<CsOrderMemberUser> list = csOrderMemberUserService.selectTeamMemberList(csOrderMemberUser);
        return getDataTable(list);
    }

    /**
     * 修改用户状态
     */
    @PreAuthorize("@ss.hasPermi('cs:csOrderMemberUser:edit')")
    @Log(title = "修改用户状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateUserStatus")
    public AjaxResult updateUserStatus(@RequestBody CsOrderMemberUser csOrderMemberUser)
    {
        return toAjax(csOrderMemberUserService.updateUserStatus(csOrderMemberUser));
    }
}
