package com.tour.api.service.impl; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.tour.api.service.ApiAgentCenterService; import com.tour.api.service.ApiMemberService; import com.tour.api.vo.AgentVo; import com.tour.api.vo.LoginVo; import com.tour.common.core.domain.entity.SysUser; import com.tour.common.core.domain.model.LoginBody; import com.tour.common.enums.UserStatus; import com.tour.common.exception.CustomException; import com.tour.common.utils.SecurityUtils; import com.tour.common.utils.StringUtils; import com.tour.common.utils.sign.Md5Utils; import com.tour.framework.web.service.TokenService; import com.tour.module.domain.*; import com.tour.module.service.*; import com.tour.module.vo.CardListVo; import com.tour.module.vo.CardVo; import com.tour.module.vo.OrderCommissonVo; import com.tour.system.service.ISysUserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 会员相关 * * @author Leong * @description * @date 2021/1/20 */ @Slf4j @Service @RequiredArgsConstructor(onConstructor_ = @Autowired) public class ApiAgentCenterServiceImpl implements ApiAgentCenterService { private final ICardService iCardService; private final IOrderService iOrderService; private final IContactsService iContactsService; private final IAgentWithdrawalService iAgentWithdrawalService; private final IAgentService agentService; private final IOrderCommissonService orderCommissonService; private final ISysUserService userService; @Override public Map getUseCard(Long agentId) { Map map = new HashMap<>(); List list = iCardService.list(new QueryWrapper().eq("agent_id", agentId)); if (!CollectionUtils.isEmpty(list)) { List noUse = list.stream().filter(item -> "0".equals(item.getStatus())).collect(Collectors.toList()); List use = list.stream().filter(item -> "1".equals(item.getStatus())).collect(Collectors.toList()); map.put("noUseNum", noUse.size()); map.put("useNum", use.size()); map.put("totalNum", list.size()); } else { map.put("noUseNum", 0); map.put("useNum", 0); map.put("totalNum", 0); } return map; } @Override public List cardList(Long agentId, String status) { Card card = new Card(); card.setAgentId(agentId); if (!"10".equals(status)) { card.setStatus(status); } List res = new ArrayList<>(); List list = iCardService.queryList(card); if (!CollectionUtils.isEmpty(list)) { list.forEach(i -> { CardListVo vo = new CardListVo(); BeanUtils.copyProperties(i, vo); Order order = new Order(); order.setCardKey(i.getCardKey()); List orders = iOrderService.queryList(order); if (!CollectionUtils.isEmpty(orders)) { vo.setCreateTime(orders.get(0).getCreateTime()); } res.add(vo); }); } return res; } @Override public CardVo getCardOrder(String cardKey) { CardVo vo = new CardVo(); Card card = iCardService.getOne(new QueryWrapper().eq("card_key", cardKey)); if (card == null) { throw new CustomException("卡不存在"); } if (card != null && !"1".equals(card.getStatus())) { throw new CustomException("卡未激活"); } BeanUtils.copyProperties(card, vo); Order order = new Order(); order.setCardKey(cardKey); List list = iOrderService.queryList(order); if (!CollectionUtils.isEmpty(list)) { vo.setCreateTime(list.get(0).getCreateTime()); vo.setTravelDetail(list.get(0).getTravelDetail()); String contactIds = list.get(0).getTravelerIds(); List ids = Arrays.asList(contactIds.split(",")); List contactsList = iContactsService.list(new QueryWrapper().in("id", ids)); vo.setContactsList(contactsList); } return vo; } @Override public Boolean withDrawal(AgentWithdrawal agentWithdrawal) { Agent agent = agentService.getById(agentWithdrawal.getAgentId()); if (agent.getAmount().compareTo(agentWithdrawal.getAmount()) < 0) { throw new CustomException("提现金额超过可提现金额"); } agent.setAmount(agent.getAmount().subtract(agentWithdrawal.getAmount())); agent.setWithdrawalAmount(agent.getWithdrawalAmount().add(agentWithdrawal.getAmount())); agentService.updateAgent(agent); Boolean result = iAgentWithdrawalService.save(agentWithdrawal); return result; } @Override public List withDrawalList(Long agentId, Integer status) { AgentWithdrawal agentWithdrawal = new AgentWithdrawal(); agentWithdrawal.setAgentId(agentId); if (status != 10) { agentWithdrawal.setStatus(status); } List list = iAgentWithdrawalService.queryList(agentWithdrawal); return list; } @Override public List commissonList(Long agentId) { return orderCommissonService.queryListByAgentId(agentId); } @Override public List getGroupUser(Long agentId) { AgentVo vo = new AgentVo(); List list = new ArrayList<>(); Agent agent = agentService.getById(agentId); BeanUtils.copyProperties(agent, vo); vo.setTotalAmount(agent.getAmount().add(agent.getWithdrawalAmount())); List subAgents = agentService.list(new QueryWrapper().eq("pid", agentId)); if (!CollectionUtils.isEmpty(subAgents)) { BigDecimal subAmount = subAgents.stream().map(i -> { return i.getAmount().add(i.getWithdrawalAmount()); }).reduce(BigDecimal.ZERO, BigDecimal::add); vo.setSubAmount(subAmount); list.add(vo); subAgents.forEach(i -> { AgentVo vo1 = new AgentVo(); BeanUtils.copyProperties(i, vo1); vo1.setTotalAmount(i.getAmount().add(i.getWithdrawalAmount())); List sAgents = agentService.list(new QueryWrapper().eq("pid", i.getId())); if (!CollectionUtils.isEmpty(sAgents)) { BigDecimal sAmount = sAgents.stream().map(item -> { return item.getAmount().add(item.getWithdrawalAmount()); }).reduce(BigDecimal.ZERO, BigDecimal::add); vo1.setSubAmount(sAmount); list.add(vo1); } }); } else { Agent agent1 = agentService.getById(agent.getPid()); if (agent1 != null) { vo.setPAgentName(agent1.getPAgentName()); } } return list; } @Override public int updateAgentPass(Long agentId, String oldPassword, String password) { SysUser user = userService.queryByAgentId(agentId); if (user==null) { throw new CustomException("用户不存在"); } else if(!SecurityUtils.matchesPassword(oldPassword,user.getPassword())){ throw new CustomException("对不起,原密码错误"); } user.setPassword(SecurityUtils.encryptPassword(password)); return userService.resetPwd(user); } }