WallboxApplyController.java 14.2 KB
Newer Older
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
package com.boco.nbd.wios.manage.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.boco.nbd.wios.manage.client.DepponClient;
import com.boco.nbd.wios.manage.convert.WallboxApplyConvert;
import com.boco.nbd.wios.manage.entity.bo.Order;
import com.boco.nbd.wios.manage.entity.bo.Supplier;
import com.boco.nbd.wios.manage.entity.bo.SupplierStaff;
import com.boco.nbd.wios.manage.entity.bo.WallboxApply;
import com.boco.nbd.wios.manage.entity.cams.enums.OrderStatus;
import com.boco.nbd.wios.manage.entity.dto.WallboxApplyBatchCheckDTO;
import com.boco.nbd.wios.manage.entity.vo.*;
import com.boco.nbd.wios.manage.service.impl.*;
import com.boco.nbd.wios.utils.ExcelUtils;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.support.session.SessionInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 墙盒申请Controller
 * <p>
 *
 * @author <a href="mail to: ning.chai@foxmail.com" rel="nofollow">chaining</a>
 */
@RestController
@Api(tags = "墙盒申请接口")
@RequestMapping("api/wallbox/apply")
public class WallboxApplyController {
    @Autowired
    private WallboxApplyService wallboxApplyService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SupplierStaffService supplierStaffService;
    @Autowired
    private SupplierService supplierService;
    @Resource
    private DepponClient depponClient;

    @ApiOperation(value = "墙盒申请")
    @PostMapping("/add")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单id", dataType = "string", paramType = "query",
            required = true), @ApiImplicitParam(name = "ifEmergency", value = "是否紧急0否,1是", dataType = "int",
            paramType = "query", required = true),})
68
    public BaseResponse<Object> applyInstallation(String orderId, Integer ifEmergency,String receiverWarehouseId) {
69 70 71 72 73 74
        Assert.notNull(orderId, "订单id不能为空");
        Order order = orderService.get(orderId);
        if (ObjectUtil.isNull(order)) {
            throw new ServiceException("订单不存在");
        }
        Integer status = order.getStatus();
75 76
        // 检查订单状态是否在1-43之间
        boolean allowed = status >= OrderStatus.DISPATCHING.getType() && status <= OrderStatus.INSTALLING.getType();
77
        if (!allowed) {
78
            throw new ServiceException("订单派单之后才能进行墙盒申请");
79
        }
80 81 82

        SessionInfo loginUser = tokenService.getUser();
        String userId = loginUser.getUserId();
83 84 85 86 87
        SupplierStaff infoByAccount = supplierStaffService.getByAccountId(Integer.valueOf(userId));
        Supplier supplier = supplierService.getById(infoByAccount.getSupplierId());
        if (supplier == null) {
            throw new ServiceException("供应商不存在");
        }
88
        WallboxApply wa = new WallboxApply();
89 90
        wa.setSupplierId(Long.valueOf(supplier.getId()));
        wa.setSupplierTeam(supplier.getName());
91 92 93
        wa.setOrderId(orderId);
        wa.setIfEmergency(ifEmergency);
        wa.setCreateAccount(tokenService.getUser().getUserId());
94
        wa.setReceiverWarehouseId(receiverWarehouseId);
95 96 97 98 99 100 101 102 103
        wallboxApplyService.insertWallboxApply(wa);
        return new BaseResponse<>(true);

    }

    /**
     * 查询墙盒申请列表
     */
    @GetMapping("/list")
104
    @ApiOperation(value = "分页查询墙盒申请列表-总仓")
105 106 107 108 109 110 111 112
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "一页显示数量", dataType = "int", paramType = "query",
            required = true), @ApiImplicitParam(name = "pagecount", value = "页码", dataType = "int", paramType =
            "query", required = true)})
    public BaseResponse<List<WallboxApplyInstallListVO>> list(WallboxApplyReqVO req) {
        List<WallboxApplyInstallListVO> list =
                wallboxApplyService.selectWallboxApplyListFromOrder(WallboxApplyConvert.INSTANCE.convert(req));
        LocalDateTime currentTime = LocalDateTime.now();
        for (WallboxApplyInstallListVO apply : list) {
113
            apply.setNumber("1");
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
            Date createdDate = apply.getCreatedTime();
            LocalDateTime createdTime = LocalDateTime.ofInstant(createdDate.toInstant(), ZoneId.systemDefault());
            LocalDateTime endTime = createdTime.plusHours(24);

            long remainingHours = currentTime.until(endTime, ChronoUnit.HOURS);
            remainingHours = Math.max(remainingHours, 0);  // 将剩余小时数限制在0-24之间
            if (remainingHours == 0) {
                apply.setIfTimeout(1L);
            } else {
                apply.setIfTimeout(0L);
            }
            apply.setTimeRemaining(Long.toString(remainingHours));
        }
        return new BaseResponse<>(list);
    }

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
    /**
     * 墙盒申请安装团队分页查询
     */
    @GetMapping("/supplierList")
    @ApiOperation(value = "分页查询墙盒申请列表-安装团队")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "一页显示数量", dataType = "int", paramType = "query",
            required = true), @ApiImplicitParam(name = "pagecount", value = "页码", dataType = "int", paramType =
            "query", required = true)})
    public BaseResponse<List<WallboxApplyInstallListVO>> supplierList(WallboxApplyReqVO req) {
        SessionInfo loginUser = tokenService.getUser();
        String userId = loginUser.getUserId();

        SupplierStaff infoByAccount = supplierStaffService.getByAccountId(Integer.valueOf(userId));
        Supplier supplier = supplierService.getById(infoByAccount.getSupplierId());
        if (supplier == null) {
            throw new ServiceException("供应商不存在");
        }
        req.setSupplierId(supplier.getId());
        List<WallboxApplyInstallListVO> list =
                wallboxApplyService.selectWallboxApplyListFromOrder(WallboxApplyConvert.INSTANCE.convert(req));
        LocalDateTime currentTime = LocalDateTime.now();
        for (WallboxApplyInstallListVO apply : list) {
            Date createdDate = apply.getCreatedTime();
            LocalDateTime createdTime = LocalDateTime.ofInstant(createdDate.toInstant(), ZoneId.systemDefault());
            LocalDateTime endTime = createdTime.plusHours(24);

            long remainingHours = currentTime.until(endTime, ChronoUnit.HOURS);
            remainingHours = Math.max(remainingHours, 0);  // 将剩余小时数限制在0-24之间
            if (remainingHours == 0) {
                apply.setIfTimeout(1L);
            } else {
                apply.setIfTimeout(0L);
            }
            apply.setTimeRemaining(Long.toString(remainingHours));
        }
        return new BaseResponse<>(list);
    }

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

    @GetMapping("/get-import-template")
    @ApiOperation("获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        WallboxApplyExcelVO excelDemo = new WallboxApplyExcelVO();
        excelDemo.setIfEmergency(0);
        excelDemo.setOrderId("CA20201204044558");
        List<WallboxApplyExcelVO> list = Arrays.asList(
                excelDemo
        );
        // 输出
        ExcelUtils.write(response, "墙盒申请导入模板.xls", "墙盒申请", WallboxApplyExcelVO.class, list);
    }

    @PostMapping("/import")
    @ApiOperation("导入墙盒申请")
    public BaseResponse<WallboxApplyExcelVO> importExcel(@RequestParam(value = "file") MultipartFile file
    ) throws Exception {
        List<WallboxApplyExcelVO> list = ExcelUtils.read(file, WallboxApplyExcelVO.class);
        return new BaseResponse<>();
    }

    /**
     * 导出墙盒申请模板
     */
    @PostMapping("/export")
    @ApiOperation("导出墙盒申请")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "一页显示数量", dataType = "int", paramType = "query",
            required = true), @ApiImplicitParam(name = "pagecount", value = "页码", dataType = "int", paramType =
            "query", required = true)})
    public void export(HttpServletResponse response, WallboxApplyReqVO req) throws IOException {
        List<WallboxApplyInstallListVO> list =
                wallboxApplyService.selectWallboxApplyListFromOrder(WallboxApplyConvert.INSTANCE.convert(req));
        ExcelUtils.write(response, "墙盒申请数据导出.xls", "墙盒申请数据", WallboxApplyInstallListVO.class, list);
    }


    /**
     * 获取墙盒申请详细信息
     */
    @ApiOperation(value = "获取墙盒详细信息")
    @GetMapping(value = "/{id}")
    public BaseResponse<Object> getInfo(@PathVariable("id") Long id) {
        return new BaseResponse<>(wallboxApplyService.selectWallboxApplyById(id));
    }

    /**
     * 获取墙盒详细信息
     */
    @ApiOperation(value = "墙盒状态")
    @PostMapping(value = "/status")
    public BaseResponse<Object> wallboxStatus(@RequestParam("orderId") String orderId) {
        WallboxApplyDetailVO rest = wallboxApplyService.selectWallboxDetail(orderId);
        return new BaseResponse<>(rest);
    }

    @ApiOperation(value = "获取物流信息")
    @GetMapping(value = "/trace/{id}")
    public BaseResponse<Object> getTrace(@PathVariable("id") String id) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mailNo", id);
        return new BaseResponse<>(JSON.parseObject(depponClient.newTraceQuery(paramMap)));
    }

    /**
     * 新增墙盒申请
     */
    @ApiOperation(value = "批量新增")
    @PostMapping
    public BaseResponse<Object> add(@RequestBody List<WallboxApply> tWallboxApply) {
        tWallboxApply.forEach(t -> {
            wallboxApplyService.insertWallboxApply(t);
        });
        return new BaseResponse<>();
    }

    @ApiOperation(value = "查询当前账号所属团队")
    @GetMapping("/get-team")
    public BaseResponse<Object> getTeamInfo() {
        SessionInfo loginUser = tokenService.getUser();
        String userId = loginUser.getUserId();

        SupplierStaff infoByAccount = supplierStaffService.getByAccountId(Integer.valueOf(userId));
        Supplier supplier = supplierService.getById(infoByAccount.getSupplierId());
        if (supplier == null) {
            throw new ServiceException("供应商不存在");
        }
        return new BaseResponse<>(supplier);
    }

259 260 261 262 263 264 265 266 267 268 269
    @ApiOperation(value = "查询当前账号所属团队")
    @GetMapping("/getTeamOrder")
     public BaseResponse<Object> getTeamOrder(){
        SessionInfo loginUser = tokenService.getUser();
        String userId = loginUser.getUserId();

        SupplierStaff infoByAccount = supplierStaffService.getByAccountId(Integer.valueOf(userId));
        Supplier supplier = supplierService.getById(infoByAccount.getSupplierId());
        return new BaseResponse<>();
    }

270 271 272 273
    /**
     * 修改墙盒申请
     */
    @PutMapping
274
    @ApiOperation(value = "修改")
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    public BaseResponse<Object> edit(@RequestBody WallboxApply tWallboxApply) {
        return new BaseResponse<>(wallboxApplyService.updateWallboxApply(tWallboxApply));
    }


    @ApiOperation(value = "申请审核")
    @PostMapping("/check")
    public BaseResponse<Object> check(@RequestBody @Validated WallboxApplyCheckVO req) {
        wallboxApplyService.checkWallboxApply(req);
        return new BaseResponse<>();
    }

    @ApiOperation(value = "同订单多申请情况")
    @PostMapping("/unapproved")
    public BaseResponse<Object> hasUnapprovedOrders(@RequestBody @Validated WallboxApplyCheckVO req) {
        WallboxApply cond = new WallboxApply();
        cond.setOrderId(req.getOrderId());
        long count =
                wallboxApplyService.selectWallboxApplyList(cond).stream().filter(l -> l.getCheckStatus() == 1).count();
        if (count > 0) {
            throw new ServiceException("该安装订单号存在多个申请");
        }
        return new BaseResponse<>();
    }

    @ApiOperation(value = "批量申请审核")
    @PostMapping("/check-batch")
    public BaseResponse<Object> checkBatch(@RequestBody WallboxApplyBatchCheckDTO req) {
        return new BaseResponse<>(wallboxApplyService.checkBatch(req));
    }

    @ApiOperation(value = "墙盒申请状态统计")
    @PostMapping("/count")
    public BaseResponse<Object> count() {
        //查询当前团队的已审批、未审批、已发货、未发货数据
        String currentLogin = tokenService.getUser().getUserId();
        WallboxApply cond = new WallboxApply();
        cond.setCreateAccount(currentLogin);
        List<WallboxApply> list = wallboxApplyService.selectWallboxApplyList(cond);
        // 使用 Lambda 表达式统计每种情况的数据数量
        long unapprovedCount = list.stream()
                .filter(apply -> apply.getCheckStatus() == 1)
                .count();

        LocalDateTime currentDateTime = LocalDateTime.now();
        long timeoutCount = list.stream()
                .filter(apply -> apply.getCheckStatus() == 1 && Duration.between(LocalDateTimeUtil.of(apply.getCreatedTime()),
                        currentDateTime).toHours() > 24)
                .count();

        long awaitingDeliveryCount = list.stream()
                .filter(apply -> "0".equals(apply.getTrackingStatus()))
                .count();

        long deliveredCount = list.stream()
                .filter(apply -> "1".equals(apply.getTrackingStatus()))
                .count();

        Map<String, Object> rest = new HashMap<>();
        rest.put("unapprovedCount", unapprovedCount);
        rest.put("timeoutCount", timeoutCount);
        rest.put("awaitingDeliveryCount", awaitingDeliveryCount);
        rest.put("deliveredCount", deliveredCount);
        return new BaseResponse<>(rest);

    }
}