package com.ruoyi.other.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.AppUserShop;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.domain.ShopScore;
import com.ruoyi.other.mapper.ShopMapper;
import com.ruoyi.other.service.ShopScoreService;
import com.ruoyi.other.service.ShopService;
import com.ruoyi.other.vo.NearbyShopVO;
import com.ruoyi.other.vo.ShopDetailVO;
import com.ruoyi.other.vo.ShopStatistics;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* 前端控制器
*
*
* @author luodangjia
* @since 2024-11-20
*/
@RestController
@RequestMapping("/shop")
@Api("门店")
public class ShopController extends BaseController {
@Resource
private ShopService shopService;
@Resource
private AppUserClient appUserClient;
@Resource
private ShopScoreService shopScoreService;
@Resource
private TokenService tokenService;
@Resource
private SysUserClient sysUserClient;
@Resource
private ShopMapper shopMapper;
@PostMapping
@ApiOperation(value = "新增门店", tags = {"管理后台-门店管理"})
@Transactional(rollbackFor = Exception.class)
public R add(@RequestBody Shop shop){
String phone = shop.getPhone();
if (!shopService.cheUserByPhone(phone)) {
return R.fail("该手机号未注册");
}
shop.setShopAllPoint(0);
shop.setShopPoint(0);
shop.setSharePoint(0);
shop.setServerPoint(0);
shop.setUsePoint(0);
shop.setGiveawayAllMoney(BigDecimal.ZERO);
shop.setGiveawayMoney(BigDecimal.ZERO);
shop.setLowerLevelGiveawayMoney(BigDecimal.ZERO);
shop.setServerGiveawayMoney(BigDecimal.ZERO);
shop.setCanWithdrawMoney(BigDecimal.ZERO);
shop.setWithdrawMoney(BigDecimal.ZERO);
shop.setOrderNumber(0);
shop.setServerOrderNumber(0);
shop.setCustomOrderNumber(0);
shopService.save(shop);
LoginUser loginUserApplet = tokenService.getLoginUser();
AppUserShop appUserShop = new AppUserShop();
appUserShop.setAppUserId(loginUserApplet.getUserid());
appUserShop.setShopId(shop.getId());
R r = appUserClient.addAppUserShop(appUserShop);
if (R.isError(r)){
throw new RuntimeException("添加失败");
}
AppUser appUser = appUserClient.getAppUserById(loginUserApplet.getUserid());
if (appUser == null){
throw new RuntimeException("添加失败");
}
appUser.setUserType(2);
R editAppUserR = appUserClient.editAppUserById(appUser);
if (R.isError(editAppUserR)){
throw new RuntimeException("添加失败");
}
return R.ok();
}
@GetMapping("/getDetailById")
@ApiOperation(value = "门店详情", tags = {"管理后台-门店管理"})
public R getDetailById(@RequestParam("id") Long id){
Shop byId = shopService.getById(id);
return R.ok(byId);
}
@DeleteMapping("/deleteShop")
@ApiOperation(value = "删除门店", tags = {"管理后台-门店管理"})
public R deleteShop(@ApiParam("门店id") @RequestParam("id") Integer id){
shopService.removeById(id);
return R.ok();
}
@PutMapping("/editShop")
@ApiOperation(value = "门店管理-编辑门店", tags = {"管理后台-门店管理"})
public R editShop(@RequestBody Shop shop){
String phone = shop.getPhone();
if (!shopService.cheUserByPhone(phone)) {
return R.fail("该手机号未注册");
}
shopService.updateById(shop);
return R.ok();
}
@GetMapping("/list")
@ApiOperation(value = "门店列表", tags = {"管理后台-门店管理"})
public R> list(@ApiParam("页码") @RequestParam Integer pageNum,@ApiParam("每一页数据大小") Integer pageSize,Shop shop){
IPage shopIPage = shopService.getShopList(pageNum, pageSize, shop);
return R.ok(shopIPage);
}
/**
* 通过手机号校验店长
*/
@PostMapping("/cheUserByPhone")
@ApiOperation(value = "通过手机号校验店长", tags = {"管理后台-门店管理"})
public R cheUserByPhone(@RequestParam("phone") String phone) {
return R.ok(shopService.cheUserByPhone(phone));
}
/**
* 重置密码
*/
@GetMapping ("/resetPassword")
@ApiOperation(value = "重置密码", tags = {"管理后台-门店管理"})
public R resetPassword(@RequestParam(value = "ids") String ids) {
String[] idsArr = ids.split(",");
for (String id : idsArr) {
Shop shop = shopService.getById(Integer.valueOf(id));
R sysUser = sysUserClient.getSysUser(shop.getAppUserId());
SysUser userData = sysUser.getData();
userData.setPassword(getLastSixDigits(userData.getPhonenumber()));
sysUserClient.resetPassword(userData);
}
return R.ok();
}
/**
* 获取手机号的后六位。
*
* @param phoneNumber 以字符串形式提供的手机号
* @return 手机号的后六位
* @throws IllegalArgumentException 如果手机号长度不足六位
*/
public String getLastSixDigits(String phoneNumber) {
if (phoneNumber == null || phoneNumber.length() < 6) {
throw new IllegalArgumentException("手机号长度不足六位");
}
// 取字符串的最后六位
return phoneNumber.substring(phoneNumber.length() - 6);
}
/**
* 附近门店列表
*/
@GetMapping("/nearbyShopList")
@ApiOperation(value = "附近门店列表", tags = {"小程序-首页"})
public R> nearbyShopList(@ApiParam("经度") @RequestParam BigDecimal longitude,
@ApiParam("纬度") @RequestParam BigDecimal latitude) {
return R.ok(shopService.nearbyShopList(longitude, latitude));
}
@GetMapping("/shopDetail")
@ApiOperation(value = "门店详情", tags = {"小程序-首页"})
public R shopDetail(@ApiParam("门店id") @RequestParam Integer shopId,
@ApiParam("经度") @RequestParam BigDecimal longitude,
@ApiParam("纬度") @RequestParam BigDecimal latitude) {
return R.ok(shopService.getShopDetail(shopId, longitude, latitude));
}
/**
* 查询当前店长所属门店
*/
@GetMapping("/shopByUser")
@ApiOperation(value = "查询当前店长所属门店", tags = {"小程序-个人中心"}, notes = "可绑定的门店列表")
public R> shopByUser() {
R> r = appUserClient.getAppUserShop(SecurityUtils.getUserId());
if (R.isSuccess(r)){
List appUserShopList = r.getData();
if (CollectionUtils.isEmpty(appUserShopList)){
return R.ok(new ArrayList<>());
}
List shopIds = appUserShopList.stream().map(AppUserShop::getShopId).collect(Collectors.toList());
List shopList = shopService.listByIds(shopIds);
return R.ok(shopList);
}
return R.ok(new ArrayList<>());
}
/**
* 绑定门店
*/
@GetMapping("/bindShop")
@ApiOperation(value = "绑定门店", tags = {"小程序-个人中心"})
public R bindShop(@ApiParam("门店id") @RequestParam Long shopId) {
AppUser appUser = appUserClient.getAppUserById(SecurityUtils.getUserId());
appUser.setShopId(shopId);
return appUserClient.editAppUserById(appUser);
}
/**
* 门店打分
* @return
*/
@PostMapping("/shopScore")
@ApiOperation(value = "门店打分", tags = {"小程序-个人中心"})
public R shopScore(@RequestBody ShopScore shopScore) {
LoginUser loginUserApplet = tokenService.getLoginUserApplet();
shopScore.setAppUserId(loginUserApplet.getUserid());
shopScore.setCreateTime(LocalDateTime.now());
shopScoreService.save(shopScore);
return R.ok();
}
/**
* 根据id获取门店信息
* @param id
* @return
*/
@ResponseBody
@PostMapping("/getShopById")
public R getShopById(@RequestParam("id") Integer id){
Shop shop = shopService.getById(id);
return R.ok(shop);
}
@ResponseBody
@PostMapping("/getShopByUserId")
public R> getShopByUserId(@RequestParam("id") Integer id){
List list = shopService.lambdaQuery().eq(Shop::getAppUserId, id).list();
return R.ok(list);
}
/**
* 根据店铺管理员电话获取门店数据
* @param phone
* @return
*/
@ResponseBody
@PostMapping("/getShopByPhone")
public R getShopByPhone(@RequestParam("phone") String phone){
Shop one = shopService.getOne(new LambdaQueryWrapper().eq(Shop::getPhone, phone).eq(Shop::getDelFlag, 0).eq(Shop::getStatus, 1));
return R.ok(one);
}
@PostMapping("/getShopByUserIds")
public R> getShopByUserIds(@RequestBody List userIds){
List list = shopService.list(new LambdaQueryWrapper().in(Shop::getAppUserId, userIds));
return R.ok(list);
}
@GetMapping("/getShopIdByName")
R> getShopIdByName(@RequestParam String shopName){
List list = shopService.list(new LambdaQueryWrapper()
.like(Shop::getName, shopName));
return R.ok(list.stream().map(Shop::getId).collect(Collectors.toSet()));
}
/**
* 门店统计
*/
@GetMapping("/shopStatistics")
@ApiOperation(value = "门店统计", tags = {"管理后台-首页统计"})
public R shopStatistics(@ApiParam("门店名称") String shopName) {
ShopStatistics shopStatistics = shopMapper.getShopStatistics(shopName);
return R.ok(shopStatistics);
}
}