package com.prolog.cs.biz.outbound.service.impl;

import com.prolog.cs.biz.outbound.dao.*;
import com.prolog.cs.biz.outbound.model.ContainerBindTaskVO;
import com.prolog.cs.biz.outbound.service.*;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.user.UserTemplate;
import com.prolog.framework.bz.core.v1.user.UserUtils;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.DateUtils;
import com.prolog.framework.utils.MapUtils;
import com.prolog.framework.utils.StringUtils;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContainerBindTaskServiceImpl implements ContainerBindTaskService {

    private static Logger logger = LoggerFactory.getLogger(ContainerBindTaskServiceImpl.class);

    @Autowired
    private ContainerBindTaskMapper containerBindTaskMapper;

    @Autowired
    private UserTemplate userTemplate;


    @Override
    @Transactional
    public Object taskConfirm(ContainerBindTaskVO taskVO) {
        //查询托盘号是否有未完成的托盘转运任务
        UserDTO userDTO = userTemplate.getUserDTO();
        String enterpriseId = userDTO.getEnterpriseId();
        Criteria crt = Criteria.forClass(ContainerBindTaskVO.class);
        crt.restrictionBuilder().eq("containerNo",taskVO.getContainerNo()).eq("warehouseId",taskVO.getWarehouseId())
                .eq("enterpriseId",enterpriseId).eq("status","0");
        crt.buildRestriction();
        List<ContainerBindTaskVO> byCriteria = containerBindTaskMapper.findByCriteria(crt);
        if(byCriteria.size()>0){
            throw new BizException("托盘状态异常");
        }
        taskVO.setEnterpriseId(enterpriseId);
        taskVO.setStatus("0");
        taskVO.setCreator(userDTO.getId());
        taskVO.setGmtCreate(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        taskVO.setCreatorName(userDTO.getUsername());
        taskVO.setGmtModified(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        taskVO.setModifier(userDTO.getId());
        taskVO.setModifierName(userDTO.getUsername());
        containerBindTaskMapper.save(taskVO);
        return taskVO;
    }

    @Override
    public List<ContainerBindTaskVO> queryList(ContainerBindTaskVO containerBindTaskVO) {
        if(StringUtils.isEmpty(containerBindTaskVO.getContainerNo())){
            throw new BizException("托盘号不能为空！");
        }
        List<ContainerBindTaskVO> byCriteria = new ArrayList<>();
        Criteria crt = Criteria.forClass(ContainerBindTaskVO.class);
        crt.restrictionBuilder().eq("containerNo",containerBindTaskVO.getContainerNo()).eq("status","0");
        crt.buildRestriction();
        byCriteria = containerBindTaskMapper.findByCriteria(crt);
        return byCriteria;
    }

    @Override
    public Object updateTaskStatus(List<String> taskIds) {
        Criteria crt = Criteria.forClass(ContainerBindTaskVO.class);
        crt.restrictionBuilder().in("taskId",taskIds.toArray());
        crt.buildRestriction();
        long status = containerBindTaskMapper.updateMapByCriteria(MapUtils.put("status", "1").getMap(), crt);
        return status;
    }
}
