xuhy
2024-10-29 76561071d3441cd8a551772d0f43d93c9f9a1e6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.ruoyi.web.controller.api;
 
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TBoard;
import com.ruoyi.system.domain.TShop;
import com.ruoyi.system.dto.TShopDTO;
import com.ruoyi.system.query.TShopQuery;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TBoardService;
import com.ruoyi.system.service.TShopService;
import com.ruoyi.system.vo.TShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Objects;
 
import static com.ruoyi.common.core.domain.AjaxResult.error;
 
/**
 * <p>
 * 店铺信息 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-14
 */
@Api(tags = "店铺信息")
@RestController
@RequestMapping("/t-shop")
public class TShopController {
 
    private final TShopService shopService;
    private final ISysUserService userService;
    private final TBoardService boardService;
    private final TokenService tokenService;
 
    @Autowired
    public TShopController(TShopService shopService, ISysUserService userService, TBoardService boardService, TokenService tokenService) {
        this.shopService = shopService;
        this.userService = userService;
        this.boardService = boardService;
        this.tokenService = tokenService;
    }
 
    /**
     * 查询店铺信息分页列表
     */
    @ApiOperation( value = "查询店铺信息分页列表")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TShopVO>> pageList(@RequestBody TShopQuery query) {
        return AjaxResult.success(shopService.pageList(query));
    }
 
    /**
     * 添加店铺信息
     */
    @ApiOperation( value = "添加店铺信息")
    @PostMapping(value = "/add")
    public AjaxResult<String> add(@RequestBody TShopDTO dto) {
        SysUser user = new SysUser();
        user.setUserName(dto.getAccount());
        user.setPhonenumber(dto.getAccount());
        if (!userService.checkUserNameUnique(user))
        {
            return error("添加用户'" + user.getUserName() + "'失败,登录账号已存在");
        }
        shopService.save(dto);
        // 添加账号
        Long userId = userService.addAccount(dto);
        dto.setUserId(userId);
        shopService.updateById(dto);
        return AjaxResult.success();
    }
 
    /**
     * 编辑店铺信息
     */
    @ApiOperation( value = "编辑店铺信息")
    @PostMapping(value = "/edit")
    public AjaxResult<String> edit(@RequestBody TShopDTO dto) {
        SysUser user = new SysUser();
        user.setUserName(dto.getAccount());
        user.setPhonenumber(dto.getAccount());
        user.setUserId(dto.getUserId());
        if (!userService.checkUserNameUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在");
        }
        shopService.updateById(dto);
        userService.updateAccount(dto);
        return AjaxResult.success();
    }
 
    /**
     * 查看店铺信息详情
     */
    @ApiOperation( value = "查看店铺信息详情")
    @GetMapping(value = "/getDetailById")
    public AjaxResult<TShopVO> getDetailById(@RequestParam("id") Long id) {
        TShop shop = shopService.getById(id);
        TShopVO shopVO = new TShopVO();
        BeanUtils.copyProperties(shop,shopVO);
        List<TBoard> list = boardService.list(Wrappers.lambdaQuery(TBoard.class)
                .eq(TBoard::getShopId, id));
        shopVO.setBoards(list);
        return AjaxResult.success(shopVO);
    }
 
    /**
     * 用户查看店铺信息详情
     */
    @ApiOperation( value = "用户查看店铺信息详情")
    @GetMapping(value = "/getDetailByUserId")
    public AjaxResult<TShop> getDetailByUserId() {
        Integer roleType = tokenService.getLoginUser().getRoleType();
        if(roleType == 1){
            return error("该用户不是商家账号");
        }
        return AjaxResult.success(shopService.getOne(Wrappers.<TShop>lambdaQuery().eq(TShop::getUserId, tokenService.getLoginUser().getUserId())
                .last("LIMIT 1")));
    }
 
    /**
     * 删除店铺信息
     */
    @ApiOperation( value = "删除店铺信息")
    @DeleteMapping(value = "/deleteById")
    public AjaxResult<Boolean> deleteById(@RequestParam("id") Long id) {
        TShop shop = shopService.getById(id);
        // 查询账号
        SysUser sysUser = userService.selectUserByUserName(shop.getAccount());
        if(Objects.nonNull(sysUser)){
            userService.deleteUserById(sysUser.getUserId());
        }
        return AjaxResult.success(shopService.removeById(id));
    }
 
    /**
     * 启用禁用店铺
     */
    @ApiOperation( value = "启用禁用店铺")
    @GetMapping(value = "/updateStatus")
    public AjaxResult<Boolean> updateStatus(@RequestParam(value = "id") Integer id,
                                            @RequestParam(value = "status") Integer status) {
        TShop shop = shopService.getById(id);
        shop.setStatus(status);
        shopService.updateById(shop);
        SysUser sysUser = userService.selectUserByUserName(shop.getAccount());
        if(Objects.nonNull(sysUser)){
            if(status == 1){
                sysUser.setStatus("0");
            }else {
                sysUser.setStatus("1");
            }
            userService.updateUser(sysUser);
        }
        return AjaxResult.success();
    }
 
}