xiaoyong931011
2022-05-16 671660360e591c153ab16f99205930b283baaa1d
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.matrix.system.padApi.action;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.*;
import com.matrix.system.app.vo.UserInfoVo;
import com.matrix.system.common.authority.DefaultAuthorityManager;
import com.matrix.system.common.authority.strategy.AccountPasswordLogin;
import com.matrix.system.common.authority.strategy.LoginStrategy;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.dao.SysCompanyDao;
import com.matrix.system.common.dao.SysUsersDao;
import com.matrix.system.common.init.LocalCache;
import com.matrix.system.common.service.SysUsersService;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.action.BaseController;
import com.matrix.system.hive.bean.SysBeauticianState;
import com.matrix.system.hive.bean.SysBedInfo;
import com.matrix.system.hive.bean.SysProjServices;
import com.matrix.system.hive.bean.SysShopInfo;
import com.matrix.system.hive.dao.SysBedInfoDao;
import com.matrix.system.hive.dao.SysShopInfoDao;
import com.matrix.system.hive.service.SysBedInfoService;
import com.matrix.system.hive.service.SysOrderService;
import com.matrix.system.hive.service.SysShopInfoService;
import com.matrix.system.hive.service.SysWorktimeService;
import com.matrix.system.padApi.dto.Base64UploadDto;
import com.matrix.system.padApi.dto.PadOrderListDto;
import com.matrix.system.padApi.dto.WebPadLoginDto;
import com.matrix.system.padApi.vo.PadOrderDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@CrossOrigin(origins = "*")
@Api(value = "PadApiCommonAction", tags = "pad端公共请求类(含登陆)")
@RestController
@RequestMapping(value = "/api/common/pad")
public class PadApiCommonAction extends BaseController {
 
    @Autowired
    private SysUsersService sysUsersService;
 
    @Autowired
    private DefaultAuthorityManager authorityManager;
 
    @Autowired
    SysShopInfoDao sysShopInfoDao;
 
    @Autowired
    SysCompanyDao sysCompanyDao;
 
    @Autowired
    private SysShopInfoService sysShopInfoService;
 
    @Resource
    private SysOrderService sysOrderService;
 
    @Resource
    private SysBedInfoService bedInfoService;
 
    @Resource
    private SysWorktimeService sysWorkTimeService;
 
    @Resource
    private SysUsersService usersService;
 
    @Autowired
    private SysBedInfoDao sysBedInfoDao;
 
    @Value("${login_public_key}")
    private String publicKey;
    @Autowired
    private SysUsersDao sysUsersDao;
 
 
    Logger log = Logger.getLogger(PadApiCommonAction.class);
 
    @Value("${file_storage_path}")
    private String fileStoragePath;
    @Value("${static_resource_url}")
    private String nginxUrl;
 
    /**
     * 最大值
     */
    private Long maxSize = 1024*1024*100L;
 
    @ApiOperation(value = "登陆接口", notes = "pad端登陆接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK",  response = UserInfoVo.class)
    })
    @PostMapping(value = "/login")
    public AjaxResult dologin(@RequestBody @Validated WebPadLoginDto loginDto)  throws Exception {
 
        SysUsers user = new SysUsers();
        user.setSuAccount(loginDto.getUsername());
        user.setSuPassword(loginDto.getPassword());
        LoginStrategy apLogin = new AccountPasswordLogin(user, sysUsersService);
        user = authorityManager.login(apLogin);
 
        UserInfoVo userInfoVo = new UserInfoVo();
        if(user.getShopId()!=null){
            SysShopInfo shopInfo = sysShopInfoService.findById(user.getShopId());
            user.setShopName(shopInfo.getShopName());
            userInfoVo.setShopName(shopInfo.getShopShortName());
        }
        user.setSuPassword(null);
 
        String token = UUIDUtil.getRandomID();
        LocalCache.save(token, user);
 
        userInfoVo.setId(user.getSuId());
        userInfoVo.setName(user.getSuName());
        userInfoVo.setRoleName(user.getRoleName());
        userInfoVo.setPhoto(user.getSuPhoto());
 
        String sb = token + "_1" + "_/api/vip/findVipInfoById/972";
        AjaxResult result = AjaxResult.buildSuccessInstance("登陆成功");
        authorityManager.initUserPower(result);
        result.putInMap("user", userInfoVo);
        result.putInMap("token", token);
        result.putInMap("rasToken", RSAUtils.encryptByPublicKey(sb, publicKey));
        return result;
    }
 
 
 
    @ApiOperation(value = "首页", notes = "首页")
    @GetMapping(value = "/frist/{suId}")
    public AjaxResult frist(@PathVariable Long suId) {
        SysUsers user = usersService.findById(suId);
        LogUtil.info("首页 shopId={}", user.getShopId());
//        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        PadOrderListDto orderListDto = new PadOrderListDto();
        orderListDto.setPageNum(1);
        orderListDto.setPageSize(5);
        int offset = (orderListDto.getPageNum() - 1) * orderListDto.getPageSize();
        int limit = orderListDto.getPageSize();
        PaginationVO pageVo = new PaginationVO();
        pageVo.setOffset(offset);
        pageVo.setLimit(limit);
        if(ObjectUtil.isNotEmpty(user.getShopId())){
            orderListDto.setShopId(user.getShopId());
        }
//        if (!DataAuthUtil.hasAllShopAuth()) {
//            orderListDto.setShopId(user.getShopId());
//        }
 
        if(ObjectUtil.isNotEmpty(user.getCompanyId())){
            orderListDto.setCompanyId(user.getCompanyId());
        }
//        QueryUtil.setQueryLimitCom(orderListDto);
        List<PadOrderDetailVo> padApiOrderListInPage = sysOrderService.findPadApiOrderListInPage(orderListDto, pageVo);
 
        AjaxResult result = AjaxResult.buildSuccessInstance("");
        result.putInMap("orderList",padApiOrderListInPage);
 
        Date date = new Date();
        SysUsers shopstaffInfo = new SysUsers();
        shopstaffInfo.setCompanyId(user.getCompanyId());
        shopstaffInfo.setShopId(user.getShopId());
        shopstaffInfo.setRoleName(Dictionary.STAFF_POST_MLS);
        List<SysUsers> mls = sysUsersDao.selectByRoleName(shopstaffInfo);
        LogUtil.info("美疗师 size={}", mls.size());
//        List<SysUsers> mls = usersService.findByRoleName(true, Dictionary.STAFF_POST_MLS);
        if(CollUtil.isNotEmpty(mls)){
            SysBeauticianState sysBeauticianState = new SysBeauticianState();
            sysBeauticianState.setBeginTime(DateUtil.getStartDate(date));
            sysBeauticianState.setEndTime(DateUtil.getStartDate(date));
            String panBanCodes = DateUtil.dateToString(sysBeauticianState.getBeginTime(),DateUtil.DATE_FORMAT_NO_SPLITE_DD);
            List<SysUsers> staffs=new ArrayList<>();
//            if(sysWorkTimeService.isInWorkTime(user.getShopId(),sysBeauticianState.getBeginTime(),sysBeauticianState.getEndTime())){
                staffs= sysUsersService.findByCodeBeaStateShop(user.getShopId(),
                        sysBeauticianState, panBanCodes);
                if(CollUtil.isNotEmpty(staffs)){
                    result.putInMap("usedMls", mls.size() - staffs.size());
                    result.putInMap("freeMls", staffs.size());
                }else{
                    result.putInMap("usedMls", mls.size());
                    result.putInMap("freeMls", 0);
                }
//            }
        }
 
        SysBedInfo bedInfo = new SysBedInfo();
        bedInfo.setShopId(user.getShopId());
        List<SysBedInfo> totalBed = bedInfoService.findByModel(bedInfo);
        if(CollUtil.isNotEmpty(totalBed)){
            SysProjServices sysProjServices = new SysProjServices();
            sysProjServices.setShopId(user.getShopId());
 
            sysProjServices.setStartTime(DateUtil.getStartDate(date));
            sysProjServices.setEndTime(DateUtil.getStartDate(date));
            List<SysBedInfo> freeBed = sysBedInfoDao.findFreeBed(sysProjServices);
//            List<SysBedInfo> freeBed = bedInfoService.findFreeBed(sysProjServices);
            if(CollUtil.isNotEmpty(freeBed)){
                result.putInMap("usedBed", totalBed.size() - freeBed.size());
                result.putInMap("freeBed", freeBed);
            }else{
                result.putInMap("usedBed", totalBed);
                result.putInMap("freeBed", 0);
            }
        }
        return result;
    }
 
    @ApiOperation(value = "用户退出系统", notes = "用户退出系统")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AjaxResult.class)
    })
    @GetMapping(value = "/loginOut")
    public AjaxResult loginOut() {
        authorityManager.getLoginOut();
        return AjaxResult.buildSuccessInstance("退出系统成功");
    }
 
    /**
     * 文件上传方法
     */
    @ApiOperation(value = "文件上传方法", notes = "文件上传方法")
    @PostMapping(value = "/doUpload")
    public AjaxResult doFileUpload(@RequestBody @Validated Base64UploadDto uploadDto){
        // 文件保存目录路径
        String savePath = fileStoragePath;
        // 文件保存目录URL
        String saveUrl = nginxUrl;
        // 保存和访问路径检查
        if (StringUtils.isBlank(saveUrl) || StringUtils.isBlank(savePath)) {
            return AjaxResult.buildFailInstance("文件上传失败错误代码:001");
        }
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        log.info("uploadDto:" + uploadDto.getBase64Str());
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = new byte[0];
        try {
            bytes = decoder.decodeBuffer(uploadDto.getBase64Str());
        } catch (IOException e) {
            return AjaxResult.buildFailInstance("上传文件失败");
        }
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + "/";
        saveUrl += ymd + "/";
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        if (bytes.length > maxSize) {
            return AjaxResult.buildFailInstance("上传文件大小超过限制");
        }
        String newFileName = UUIDUtil.getRandomID() + UUIDUtil.getRandomID() + ".png";
        File uploadedFile = new File(savePath, newFileName);
        try {
            FileCopyUtils.copy(bytes, uploadedFile);
        } catch (Exception e) {
            return AjaxResult.buildFailInstance("上传文件失败");
        }
        log.info("saveUrl:" + saveUrl);
        String visitPath = saveUrl + newFileName;
        log.info("上传一个文件:" + newFileName);
        log.info("访问路径:" + visitPath);
 
        AjaxResult result = AjaxResult.buildSuccessInstance("上传成功");
        result.putInMap("path", visitPath);
        result.putInMap("fileName", newFileName);
        return result;
    }
 
 
}