18582019636
2024-06-25 ca497f8389174bb81e91f840cab70c9579bcdd29
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
package com.ruoyi.admin.controller;
 
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.entity.Evaluate;
import com.ruoyi.admin.entity.MasterWorker;
import com.ruoyi.admin.service.EvaluateService;
import com.ruoyi.admin.service.MasterWorkerService;
import com.ruoyi.admin.vo.MasterWorkerDetailVO;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 师傅信息表 前端控制器
 * </p>
 *
 * @author hjl
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/masterWorker")
@Api(tags = {"后台-师傅管理-师傅列表管理"})
public class MasterWorkerController {
 
    @Resource
    private MasterWorkerService masterWorkerService;
    @Resource
    private EvaluateService evaluateService;
 
    /**
     * 师傅列表分页查询 (添加订单-选择师傅列表)
     */
    @ApiOperation(value = "师傅列表", tags = {"后台-师傅管理"})
    @GetMapping(value = "/selectServe")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "师傅姓名", name = "workerName", dataType = "String"),
            @ApiImplicitParam(value = "师傅电话", name = "workerPhone", dataType = "String"),
            @ApiImplicitParam(value = "服务城市", name = "city", dataType = "String"),
            @ApiImplicitParam(value = "页码", name = "pageNum", dataType = "Integer", required = true),
            @ApiImplicitParam(value = "每页显示条数", name = "pageSize", dataType = "Integer", required = true)
    })
    public R<IPage<MasterWorker>> selectServe(String workerName, String workerPhone, String city,
                                              @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryChainWrapper<MasterWorker> wrapper = masterWorkerService.lambdaQuery();
        wrapper = StringUtils.isNotBlank(workerName) ? wrapper.like(MasterWorker::getRealName, workerName) : wrapper;
        wrapper = StringUtils.isNotBlank(workerPhone) ? wrapper.like(MasterWorker::getPhone, workerPhone) : wrapper;
        wrapper = StringUtils.isNotBlank(city) ? wrapper.like(MasterWorker::getCity, city) : wrapper;
        return R.ok(wrapper.eq(MasterWorker::getIsDelete, 0)
                .orderByDesc(MasterWorker::getCreateTime).page(Page.of(pageNum, pageSize)));
    }
 
    /**
     * 新增师傅信息
     *
     * @param masterWorker 师傅详细信息
     */
    @ApiOperation(value = "新增师傅信息", tags = {"后台-师傅管理-师傅列表管理"})
    @PostMapping(value = "/save")
    public R<String> save(@RequestBody MasterWorker masterWorker) {
        // MD5加密登录密码(新)
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String md5Password = passwordEncoder.encode(Constants.DEFAULT_PASSWORD);
        masterWorker.setPassword(md5Password);
        return masterWorkerService.save(masterWorker) ? R.ok() : R.fail();
    }
 
    /**
     * 根据id批量删除账号
     *
     * @param ids 账号id拼接
     */
    @ApiOperation(value = "删除师傅信息", tags = {"后台-师傅管理-师傅列表管理"})
    @GetMapping(value = "/batchDelete")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "多条账号id ',' 拼接", name = "ids", dataType = "String", required = true)
    })
    public R<String> batchDelete(@RequestParam String ids) {
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        List<MasterWorker> list = masterWorkerService.lambdaQuery().in(MasterWorker::getId, idList).list();
        list.forEach(data -> data.setIsDelete(1));
        return masterWorkerService.updateBatchById(list) ? R.ok() : R.fail();
    }
 
    /**
     * 修改师傅信息
     *
     * @param masterWorker 师傅信息
     */
    @ApiOperation(value = "修改师傅信息", tags = {"后台-师傅管理-师傅列表管理"})
    @PostMapping(value = "/update")
    public R<String> update(@RequestBody MasterWorker masterWorker) {
        return masterWorkerService.updateById(masterWorker) ? R.ok() : R.fail();
    }
 
    /**
     * 启用/关闭师傅账号
     *
     * @param id     师傅信息id
     * @param enable 启用/关闭
     */
    @ApiOperation(value = "启用/关闭师傅账号", tags = {"后台-师傅管理-师傅列表管理"})
    @GetMapping(value = "/enable")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "师傅id", name = "id", dataType = "Integer", required = true),
            @ApiImplicitParam(value = "0:关闭;1:启用", name = "enable", dataType = "Integer", required = true)
    })
    public R<String> enable(@RequestParam Integer id, @RequestParam Integer enable) {
        boolean update = masterWorkerService.lambdaUpdate().set(MasterWorker::getIsEnable, enable)
                .eq(MasterWorker::getId, id).update();
        return update ? R.ok() : R.fail();
    }
 
    /**
     * 新增师傅信息
     *
     * @param workerId 师傅id
     */
    @ApiOperation(value = "师傅详细信息", tags = {"后台-师傅管理-师傅列表管理"})
    @GetMapping(value = "/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "师傅id", name = "workerId", dataType = "Integer", required = true)
    })
    public R<MasterWorkerDetailVO> detail(@RequestParam Integer workerId) {
        MasterWorker worker = masterWorkerService.getById(workerId);
        if (null == worker) {
            return R.fail("获取师傅信息有误!");
        }
        // 返回信息封装
        MasterWorkerDetailVO workerDetail = new MasterWorkerDetailVO();
        BeanUtils.copyProperties(worker, workerDetail);
        // 计算服务星级
        List<Evaluate> evaluateList = evaluateService.lambdaQuery().eq(Evaluate::getWorkerId, workerId)
                .eq(Evaluate::getIsDelete, 0).list();
        List<BigDecimal> star = evaluateList.stream().map(Evaluate::getStarRating).collect(Collectors.toList());
        // 总评分 / 评价数量
        BigDecimal starRating = star.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal result = starRating.divide(new BigDecimal(star.size()), 1, RoundingMode.HALF_UP);
        workerDetail.setStarRating(result);
        return R.fail(workerDetail);
    }
 
}