package com.yc.ship.module.product.service.distributorDiscount;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.ota.api.dto.DistributorRespDTO;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountPageReqVO;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.distributorDiscount.DistributorDiscountDO;
import com.yc.ship.module.product.dal.mysql.distributorDiscount.DistributorDiscountMapper;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;

/**
 * 分销商专属折扣 Service 实现类
 *
 * @author lsq
 */
@Service
@Validated
public class DistributorDiscountServiceImpl implements DistributorDiscountService {

    @Resource
    private DistributorDiscountMapper distributorDiscountMapper;

    @Resource
    private OtaDistributorApi otaDistributorApi;

    @Override
    public Long createDistributorDiscount(DistributorDiscountSaveReqVO createReqVO) {
        // 插入
        DistributorRespDTO distributor = otaDistributorApi.getDistributorById(createReqVO.getDistributorId());
        DistributorDiscountDO distributorDiscount = BeanUtils.toBean(createReqVO, DistributorDiscountDO.class);
        distributorDiscount.setId(IdWorker.getId(distributorDiscount));
        if(createReqVO.getAreaId()!=null) {
            distributorDiscount.setAreaId(createReqVO.getAreaId().toString());
        }
        if(createReqVO.getShipId()!=null){
            distributorDiscount.setShipId(createReqVO.getShipId().toString()) ;
        }

        distributorDiscount.setDistributorName(distributor.getName()) ;
        distributorDiscountMapper.insert(distributorDiscount);


        // 返回
        return distributorDiscount.getId();
    }

    @Override
    public void updateDistributorDiscount(DistributorDiscountSaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorDiscountExists(updateReqVO.getId());
        DistributorRespDTO distributor = otaDistributorApi.getDistributorById(updateReqVO.getDistributorId());
        // 更新
        DistributorDiscountDO updateObj = BeanUtils.toBean(updateReqVO, DistributorDiscountDO.class);
        if(updateReqVO.getAreaId()!=null) {
            updateObj.setAreaId(updateReqVO.getAreaId().toString());
        }
        if(updateReqVO.getShipId()!=null) {
            updateObj.setShipId(updateReqVO.getShipId().toString());
        }
        updateObj.setDistributorName(distributor.getName()) ;
        distributorDiscountMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorDiscount(Long id) {
        // 校验存在
        validateDistributorDiscountExists(id);
        // 删除
        distributorDiscountMapper.deleteById(id);
    }

    private void validateDistributorDiscountExists(Long id) {
        if (distributorDiscountMapper.selectById(id) == null) {
            throw exception0(600_01,"分销商专属折扣不存在");
        }
    }

    @Override
    public DistributorDiscountDO getDistributorDiscount(Long id) {
        return distributorDiscountMapper.selectById(id);
    }

    @Override
    public DistributorDiscountDO getByTravlId(Long id,String shipId) {
        LocalDateTime now = LocalDateTimeUtil.now();
        List<DistributorDiscountDO> distributorDiscountDOList = distributorDiscountMapper.selectList(
                 new LambdaQueryWrapperX<DistributorDiscountDO>()
                         .eq(DistributorDiscountDO::getDistributorId, id)
                         .likeIfPresent(DistributorDiscountDO::getShipId, shipId)
                         .ge(DistributorDiscountDO::getEndTime, now)
                         .le(DistributorDiscountDO::getStartTime, now)
         );
        if(distributorDiscountDOList!=null && !distributorDiscountDOList.isEmpty()){
            return distributorDiscountDOList.get(0);
        }
        return null;
    }

    @Override
    public PageResult<DistributorDiscountDO> getDistributorDiscountPage(DistributorDiscountPageReqVO pageReqVO) {
        return distributorDiscountMapper.selectPage(pageReqVO);
    }

}