package com.fs.company.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.ServiceException;
import com.fs.company.domain.CompanyUserChangeApply;
import com.fs.company.domain.CompanyUserChangeApplyUser;
import com.fs.company.mapper.CompanyUserChangeApplyMapper;
import com.fs.company.service.ICompanyUserChangeApplyService;
import com.fs.company.service.ICompanyUserChangeApplyUserService;
import com.fs.company.vo.CompanyUserChangeApplyVO;
import com.fs.store.domain.FsUser;
import com.fs.store.mapper.FsUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Service
@AllArgsConstructor
public class CompanyUserChangeApplyServiceImpl extends ServiceImpl<CompanyUserChangeApplyMapper, CompanyUserChangeApply> implements ICompanyUserChangeApplyService {

    private final ICompanyUserChangeApplyUserService companyUserChangeApplyUserService;
    private final FsUserMapper userMapper;

    /**
     * 申请更换会员归属
     *
     * @param from      原归属
     * @param to        新归属
     * @param type      类型  0全部 1部分
     * @param ids       会员
     * @param companyId 公司ID
     * @param userName  操作用户
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void apply(Long from, Long to, Integer type, List<Long> ids, Long companyId, String userName) {
        List<CompanyUserChangeApplyUser> users = new ArrayList<>();

        // 全部
        if (type == 0) {
            FsUser param = new FsUser();
            param.setCompanyUserId(from);
            List<FsUser> userList = userMapper.selectFsUserList(param);
            for (FsUser user : userList) {
                CompanyUserChangeApplyUser uu = new CompanyUserChangeApplyUser();
                uu.setUserId(user.getUserId());
                users.add(uu);
            }
        }
        // 部分
        else {
            for (Long id : ids) {
                FsUser user = userMapper.selectFsUserById(id);
                if (Objects.isNull(user)) {
                    throw new ServiceException("会员不存在");
                }

                CompanyUserChangeApplyUser uu = new CompanyUserChangeApplyUser();
                uu.setUserId(user.getUserId());
                users.add(uu);
            }
        }

        if (users.isEmpty()) {
            throw new ServiceException("不存在可更换会员");
        }

        // 添加申请
        CompanyUserChangeApply apply = new CompanyUserChangeApply();
        apply.setCompanyId(companyId);
        apply.setFrom(from);
        apply.setTo(to);
        apply.setApplyTime(LocalDateTime.now());
        apply.setApplyBy(userName);
        save(apply);

        // 添加申请关联会员
        users.forEach(user -> user.setApplyId(apply.getId()));
        companyUserChangeApplyUserService.saveBatch(users);
    }

    /**
     * 查询申请列表
     * @param map 条件
     * @return  list
     */
    @Override
    public List<CompanyUserChangeApplyVO> selectApplyListByMap(Map<String, Object> map) {
        List<CompanyUserChangeApplyVO> list = baseMapper.selectApplyListByMap(map);
        list.forEach(user -> user.setUsers(companyUserChangeApplyUserService.getApplyUsers(user.getId())));
        return list;
    }

    /**
     * 查询申请详情
     * @param id    申请记录ID
     * @return  CompanyUserChangeApplyVO
     */
    @Override
    public CompanyUserChangeApplyVO selectApplyDetailById(Long id) {
        CompanyUserChangeApplyVO companyUserChangeApplyVO = baseMapper.detailById(id);
        if (Objects.nonNull(companyUserChangeApplyVO)) {
            companyUserChangeApplyVO.setUsers(companyUserChangeApplyUserService.getApplyUsers(id));
        }
        return companyUserChangeApplyVO;
    }

    /**
     * 申请记录审核
     * @param id        申请记录ID
     * @param status    审核状态
     * @param reason    拒绝理由
     * @param username  审核人
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void audit(Long id, Integer status, String reason, String username) {
        CompanyUserChangeApply companyUserChangeApply = baseMapper.selectById(id);
        if (Objects.isNull(companyUserChangeApply)) {
            throw new ServiceException("申请记录不存在");
        }

        if (companyUserChangeApply.getStatus() != 0) {
            throw new ServiceException("请勿重复审核");
        }

        // 通过
        if (status == 1) {
            // 修改会员关联销售
            companyUserChangeApplyUserService.changeUser(id);
        }
        // 拒绝
        else {
            companyUserChangeApply.setReason(reason);
        }

        companyUserChangeApply.setAuditBy(username);
        companyUserChangeApply.setAuditTime(LocalDateTime.now());
        companyUserChangeApply.setStatus(status);
        baseMapper.updateById(companyUserChangeApply);
    }
}
