package com.yc.ship.module.ota.dal.mysql.distributor;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.ota.api.dto.DistributorRespDTO;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorListReqVO;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorSaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO;
import com.yc.ship.module.product.api.dto.ProductSpuRespDTO;
import com.yc.ship.module.trade.api.dto.StoreRespVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 分销商信息 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface DistributorMapper extends BaseMapperX<DistributorDO> {

    default DistributorDO selectByLoginName(String loginName) {
        return selectOne(DistributorDO::getLoginName, loginName,DistributorDO::getDeleted, 0);
    }

    default PageResult<DistributorDO> selectPage(DistributorPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributorDO>()
                .eqIfPresent(DistributorDO::getIsUse, reqVO.getIsUse())
                .betweenIfPresent(DistributorDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorDO::getType, reqVO.getType())
                .eqIfPresent(DistributorDO::getLoginName, reqVO.getLoginName())
                .likeIfPresent(DistributorDO::getName, reqVO.getName())
                .orderByDesc(DistributorDO::getId));
    }

    @TenantIgnore
    List<StoreRespVO> selectDistributorStore(@Param("agencyId") Long agencyId);
    @TenantIgnore
    StoreRespVO selectStore(@Param("id") Long id);
    default List<DistributorDO> getSimpleList(DistributorListReqVO reqVO) {
        LambdaQueryWrapperX<DistributorDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.likeIfPresent(DistributorDO::getName, reqVO.getName());
        if (reqVO.getType() != 0) {
            queryWrapper.eqIfPresent(DistributorDO::getType, reqVO.getType());
        }
        if(ObjectUtil.equal(reqVO.getType(), 2)) {
            //旅行社
            queryWrapper.eqIfPresent(DistributorDO::getSelfSupport, reqVO.getSelfSupport());
        }

        queryWrapper.eqIfPresent(DistributorDO::getIsUse, reqVO.getIsUse());
        return selectList(queryWrapper);
    }

    List<Map> getOtcSimpleList(@Param("params") DistributorSaveReqVO params);

    List<DistributorRespVO> getOtcDistributor(@Param("type") Integer type);
    List<DistributorRespVO> getOtcDistributorByOtaId(@Param("type") Integer type,@Param("distributorId") Long distributorId);

    List<DistributorRespVO> getOtcDistributorWithId(@Param("type") Integer type,@Param("productId") Long productId);

    @TenantIgnore
    DistributorRespVO getDistributorById(@Param("id") Long id);

    @TenantIgnore
    DistributorDO getDistributorNew(@Param("id") Long id);
    @TenantIgnore
    DistributorRespVO getById(@Param("id") Long id);

    @TenantIgnore
    DistributorRespVO getByTenantId(@Param("tenantId") Long tenantId);

    int addBalance(@Param("distributorId") Long distributorId, @Param("money") BigDecimal money);

    int subBalance(@Param("distributorId")Long distributorId, @Param("money") BigDecimal money);

    int addInventory(@Param("distributorId") Long distributorId, @Param("money") BigDecimal money);

    int addSurplus(@Param("distributorId") Long distributorId, @Param("money")BigDecimal money);
    int addPayCredit(@Param("distributorId") Long distributorId, @Param("money")BigDecimal money);
    int subPayCredit(@Param("distributorId") Long distributorId, @Param("money")BigDecimal money);
    int updateShortCode(@Param("distributorId") Long distributorId, @Param("code")String code);

    int subSurplus(@Param("distributorId")Long distributorId, @Param("money") BigDecimal money);

    List<DistributorDO> getDistributorByOtaAccountAndType(@Param("otaAccount") String otaAccount, @Param("typeCode") String typeCode);

    int subInventory(@Param("distributorId")Long distributorId, @Param("money")BigDecimal money);

    List<Long> getTenantIds();
    List<Long> getIds();


    DistributorRespVO queryDistributorMoneyTotal(@Param("distributorId")Long distributorId);

    @TenantIgnore
    List<DistributorDO> getPlatTenantId(@Param("currentTenantId") Long currentTenantId);

    @TenantIgnore
    DistributorDO getDistributorByTenantId(@Param("tenantId") Long tenantId);
    @TenantIgnore
    Long getProductId(@Param("productId") Long productId);
    default DistributorDO selectByUserId(Long loginUserId) {
        return selectOne(DistributorDO::getUserId, loginUserId);
    }

    @TenantIgnore
    IPage<ProductSpuRespDTO> getOtaProductPage(IPage<ProductSpuRespDTO> page, @Param("vo") DistributorPageReqVO vo, @Param("distributorId") Long distributorId);

    @TenantIgnore
    DistributorDO getDistributor(@Param("userName") String userName);

    @TenantIgnore
    List<ProductSpuRespDTO> selectOtaSpuList(@Param("categoryId") Long categoryId, @Param("distributorId") Long distributorId, @Param("useDate") String useDate);


    default List<DistributorDO> selectListByType(Integer type) {
        return selectList(new LambdaQueryWrapperX<DistributorDO>()
                .eqIfPresent(DistributorDO::getType, type)
                .eq(DistributorDO::getIsUse, true)
                .orderByDesc(DistributorDO::getId));
    }
    @TenantIgnore
    List<DistributorDO> selectChildNo();
}
