package org.wcs.serve.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.constant.ConstantData;
import org.wcs.constant.enums.database.EtagTaskLightModelEnum;
import org.wcs.constant.enums.database.EtagTaskStatusEnum;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppEtagTaskBakService;
import org.wcs.mapper.intf.AppEtagTaskService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagTask.AddEtagTaskReq;
import org.wcs.model.dto.serve.etagTask.EtagTaskQueryReq;
import org.wcs.model.dto.serve.etagTask.UpdateEtagTaskReq;
import org.wcs.model.po.app.AppEtagTask;
import org.wcs.model.po.app.AppEtagTaskBak;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.common.PageDataResponse;
import org.wcs.model.vo.serve.etagTask.EtagTaskBakVo;
import org.wcs.model.vo.serve.etagTask.EtagTaskVo;
import org.wcs.serve.service.intf.IEtagTaskService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;
import org.wcs.utils.AppUUIDUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 电子标签任务服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EtagTaskService implements IEtagTaskService {

    private final AppEtagTaskService etagTaskService;
    private final AppEtagTaskBakService etagTaskBakService;

    /**
     * 查询电子标签任务
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public AppServeDataResponse<PageDataResponse<EtagTaskVo>> queryEtagTask(EtagTaskQueryReq request, Integer pageIndex, Integer pageSize) {
        Tuple2<Long, List<AppEtagTask>> queryEtagTaskTuple = etagTaskService.queryEtagTask(request, pageIndex, pageSize);
        if(queryEtagTaskTuple == null) {
            return AppServeResponseFactory.fail("查询失败", null);
        }
        PageDataResponse<EtagTaskVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(queryEtagTaskTuple.item1);
        pageDataResponse.setData(AppBeanUtils.copyListProperties(queryEtagTaskTuple.item2, EtagTaskVo::new));
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }

    /**
     * 新增电子标签任务
     * @param request 新增参数
     * @return 响应
     */
    @Override
    public AppServeResponse addEtagTask(AddEtagTaskReq request) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String operationUserId = userIdObj.toString();
        log.info("用户：{} 想要添加电子标签任务：{}", operationUserId, AppStringUtils.objectToString(request));
        String taskId = AppUUIDUtils.getNewUUID();
        AppEtagTask appEtagTask = new AppEtagTask();
        appEtagTask.setTaskId(taskId);
        appEtagTask.setTaskGroup(taskId);
        appEtagTask.setTaskType(request.getTaskType());
        appEtagTask.setVehicleNo(request.getVehicleNo());
        appEtagTask.setOrderId(request.getOrderId());
        appEtagTask.setLocation(request.getLocation());
        appEtagTask.setGoodsId(request.getGoodsId());
        appEtagTask.setGoodsName(request.getGoodsName());
        appEtagTask.setLightNum(request.getLightNum());
        appEtagTask.setTaskStatus(EtagTaskStatusEnum.NOT_ACTIVATED.getCode());
        appEtagTask.setLightModel(request.getLightModel());
        appEtagTask.setTaskSource(ConstantData.SYSTEM_NAME);
        appEtagTask.setCreatePerson(operationUserId);
        appEtagTask.setCreateTime(LocalDateTime.now());
        appEtagTask.setRemark(request.getRemark());
        Integer result = etagTaskService.insert(appEtagTask).result();
        if(result > 0) {
            return AppServeResponseFactory.success("添加成功", null);
        }
        return AppServeResponseFactory.fail("添加失败");
    }

    /**
     * 修改电子标签任务
     * @param request 修改参数
     * @return 响应
     */
    @Override
    public AppServeResponse updateEtagTask(UpdateEtagTaskReq request) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录");
        }
        String operationUserId = userIdObj.toString();
        log.info("用户：{} 想要修改电子标签任务：{}", operationUserId, AppStringUtils.objectToString(request));
        AppEtagTask appEtagTask = new AppEtagTask();
        appEtagTask.setTaskId(request.getTaskId());
        appEtagTask.setTaskGroup(request.getTaskGroup());
        appEtagTask.setTaskType(request.getTaskType());
        appEtagTask.setVehicleNo(request.getVehicleNo());
        appEtagTask.setOrderId(request.getOrderId());
        appEtagTask.setLocation(request.getLocation());
        appEtagTask.setGoodsId(request.getGoodsId());
        appEtagTask.setGoodsName(request.getGoodsName());
        appEtagTask.setLightNum(request.getLightNum());
        appEtagTask.setConfirmNum(request.getConfirmNum());
        appEtagTask.setTaskStatus(request.getTaskStatus());
        appEtagTask.setLightModel(request.getLightModel());
        appEtagTask.setRemark(request.getRemark());
        Integer result = etagTaskService.updateIgnoreNull(appEtagTask).result();
        if(result > 0) {
            return AppServeResponseFactory.success("修改成功", null);
        }
        return AppServeResponseFactory.fail("修改失败，请重试");
    }

    /**
     * 查询电子标签任务备份
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public AppServeDataResponse<PageDataResponse<EtagTaskBakVo>> queryEtagTaskBak(EtagTaskQueryReq request, Integer pageIndex, Integer pageSize) {
        Tuple2<Long, List<AppEtagTaskBak>> queryEtagTaskTuple = etagTaskBakService.queryEtagTask(request, pageIndex, pageSize);
        if(queryEtagTaskTuple == null) {
            return AppServeResponseFactory.fail("查询失败", null);
        }
        PageDataResponse<EtagTaskBakVo> pageDataResponse = new PageDataResponse<>();
        pageDataResponse.setPageIndex(pageIndex);
        pageDataResponse.setPageSize(pageSize);
        pageDataResponse.setTotalCount(queryEtagTaskTuple.item1);
        pageDataResponse.setData(AppBeanUtils.copyListProperties(queryEtagTaskTuple.item2, EtagTaskBakVo::new));
        return AppServeResponseFactory.success("查询成功", pageDataResponse);
    }
}
