package com.yc.ship.module.trade.service.orderjzdetail;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.security.core.LoginUser;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.trade.controller.admin.orderjzdetail.vo.OrderJzDetailPageReqVO;
import com.yc.ship.module.trade.controller.admin.orderjzdetail.vo.OrderJzDetailSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.orderjz.OrderJzDO;
import com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO;
import com.yc.ship.module.trade.dal.mysql.order.TradeOrderMapper;
import com.yc.ship.module.trade.dal.mysql.orderjz.OrderJzMapper;
import com.yc.ship.module.trade.dal.mysql.orderjzdetail.OrderJzDetailMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * 订单接站人员名单 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class OrderJzDetailServiceImpl implements OrderJzDetailService {

  @Resource
  private TradeOrderMapper tradeOrderMapper;

  @Resource
  private OrderJzMapper orderJzMapper;

  @Resource
  private OrderJzDetailMapper orderJzDetailMapper;

  @Override
  public Long createOrderJzDetail(OrderJzDetailSaveReqVO createReqVO) {
    // 插入
    OrderJzDetailDO orderJzDetail = BeanUtils.toBean(createReqVO, OrderJzDetailDO.class);
    orderJzDetail.setTenantId(1L);
    orderJzDetail.setIdCard(createReqVO.getIdCard());
    orderJzDetailMapper.insert(orderJzDetail);
    // 返回
    return orderJzDetail.getId();
  }


  @Override
  @TenantIgnore
  public Long createOrderJzDetail(List<OrderJzDetailSaveReqVO> createReqVOList) {
    if (createReqVOList == null || createReqVOList.isEmpty()) {
      return 0L;
    }
    String oid = createReqVOList.get(0).getOid();
    if (oid == null || oid.isEmpty()) {
      throw new RuntimeException("未查询到相关订单");
    }
    TradeOrderDO orderDo = tradeOrderMapper.selectOne(new LambdaQueryWrapper<TradeOrderDO>().eq(TradeOrderDO::getId, oid));
    if (orderDo == null) {
      throw new RuntimeException("未查询到相关订单");
    }
    // 根据订单编号查询jz编号
    OrderJzDO orderJzDO = orderJzMapper.selectOne(new LambdaQueryWrapper<OrderJzDO>().eq(OrderJzDO::getOrderId, oid));
    if (orderJzDO == null) {
      orderJzDO = new OrderJzDO();
      orderJzDO.setId(IdUtil.getSnowflake(0, 0).nextId());
      orderJzDO.setIsJz(1);
      orderJzDO.setJzAddress("");
      orderJzDO.setJzLinkName("");
      orderJzDO.setJzLinkPhone("");
      orderJzDO.setOrderId(orderDo.getId());
      orderJzMapper.insert(orderJzDO);
    }

    long count = 0;
    String batchNo = IdUtil.getSnowflake(0, 0).nextIdStr();
    // 先删除上一次手机号录入的数据
    for (OrderJzDetailSaveReqVO orderJzDetailSaveReqVO : createReqVOList) {
      orderJzDetailSaveReqVO.setJzId(orderJzDO.getId());
      deleteDetailList(orderJzDetailSaveReqVO.getJzId(), orderJzDetailSaveReqVO.getIdCard());
    }
    for (OrderJzDetailSaveReqVO orderJzDetailSaveReqVO : createReqVOList) {
      orderJzDetailSaveReqVO.setBatchNo(batchNo);
      createOrderJzDetail(orderJzDetailSaveReqVO);
      count++;
    }
    return count;
  }

  private void deleteDetailList(Long jzId, String idCard) {
    LambdaQueryWrapper<OrderJzDetailDO> w1 = new LambdaQueryWrapper<>();
    w1.eq(OrderJzDetailDO::getJzId, jzId);
    w1.eq(OrderJzDetailDO::getIdCard, idCard);
    List<OrderJzDetailDO> detailList = orderJzDetailMapper.selectList(w1);
    if (detailList == null || detailList.isEmpty()) {
      return;
    }
    for (OrderJzDetailDO orderJzDetailDO : detailList) {
      orderJzDetailMapper.deleteById(orderJzDetailDO.getId());
    }
  }


  @Override
  public void updateOrderJzDetail(OrderJzDetailSaveReqVO updateReqVO) {
    // 校验存在
    validateOrderJzDetailExists(updateReqVO.getId());
    // 更新
    OrderJzDetailDO updateObj = BeanUtils.toBean(updateReqVO, OrderJzDetailDO.class);
    orderJzDetailMapper.updateById(updateObj);
  }

  @Override
  public void deleteOrderJzDetail(Long id) {
    // 校验存在
    validateOrderJzDetailExists(id);
    // 删除
    orderJzDetailMapper.deleteById(id);
  }

  private void validateOrderJzDetailExists(Long id) {
    if (orderJzDetailMapper.selectById(id) == null) {
      throw exception(500,"不存在");
    }
  }

  @Override
  public OrderJzDetailDO getOrderJzDetail(Long id) {
    return orderJzDetailMapper.selectById(id);
  }

  @Override
  public PageResult<OrderJzDetailDO> getOrderJzDetailPage(OrderJzDetailPageReqVO pageReqVO) {
    return orderJzDetailMapper.selectPage(pageReqVO);
  }

  @Override
  public List<OrderJzDetailDO> selectPageByVoyageId(String voyageId) {
    return orderJzDetailMapper.selectPageByVoyageId(voyageId);
  }

  @Override
  public List<OrderJzDetailDO> getOrderJzDetailList(OrderJzDetailPageReqVO pageReqVO) {
    String oid = pageReqVO.getOid();
    // 根据订单编号查询jz编号
    OrderJzDO orderJzDO = orderJzMapper.selectOne(new LambdaQueryWrapper<OrderJzDO>().eq(OrderJzDO::getOrderId, oid));
    if (orderJzDO == null) {
      return new ArrayList<>();
    }
    pageReqVO.setJzId(orderJzDO.getId());
    return orderJzDetailMapper.selectList(pageReqVO);
  }

  @Override
  public Long cleanOrderJzDetail(String id) {
    return orderJzDetailMapper.cleanSign(id);
  }

  @Override
  public Long signOrderJzDetail(OrderJzDetailSaveReqVO createReqVO) {
    OrderJzDetailDO detail = getOrderJzDetail(createReqVO.getId());
    if(detail != null) {
      detail.setSignTime(DateUtils.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
      // 当前登录人
      LoginUser loginUser = getLoginUser();
      detail.setSignMan(MapUtils.getString(loginUser.getInfo(), LoginUser.INFO_KEY_NICKNAME));
      detail.setSignRemark(createReqVO.getSignRemark());
      detail.setSignImage(createReqVO.getSignImage());
      orderJzDetailMapper.updateById(detail);
      return detail.getId();
    }
    return 0L;
  }

  @Override
  public PageResult<OrderJzDetailDO> getOrderJzDetailPage2(OrderJzDetailPageReqVO pageReqVO) {
    IPage<OrderJzDetailDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
    IPage<OrderJzDetailDO> iPage = orderJzDetailMapper.selectPage2(page, pageReqVO);
    return new PageResult<>(iPage.getRecords(), iPage.getTotal());
  }
}