package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.enums.StatusEnum;
import com.fuint.common.param.RaffleTypePage;
import com.fuint.common.service.RaffleTypeService;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.repository.mapper.MtRaffleTypeMapper;
import com.fuint.repository.model.MtRaffleType;
import com.fuint.utils.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

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

/**
 * 抽奖类型服务实现类
 *
 * Created by FSQ
 * CopyRight https://www.fuint.cn
 */
@Service
public class RaffleTypeServiceImpl extends ServiceImpl<MtRaffleTypeMapper, MtRaffleType> implements RaffleTypeService {

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

    private MtRaffleTypeMapper mtRaffleTypeMapper;

    public RaffleTypeServiceImpl(MtRaffleTypeMapper mtRaffleTypeMapper) {
        this.mtRaffleTypeMapper = mtRaffleTypeMapper;
    }

    @Override
    public PaginationResponse<MtRaffleType> queryRaffleTypeListByPagination(RaffleTypePage page) {
        Page<MtRaffleType> pageHelper = PageHelper.startPage(page.getPage(), page.getPageSize());
        LambdaQueryWrapper<MtRaffleType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.ne(MtRaffleType::getStatus, StatusEnum.DISABLE.getKey());

        if (StringUtil.isNotEmpty(page.getName())) {
            lambdaQueryWrapper.like(MtRaffleType::getName, page.getName());
        }
        if (StringUtil.isNotEmpty(page.getCode())) {
            lambdaQueryWrapper.like(MtRaffleType::getCode, page.getCode());
        }
        if (StringUtil.isNotEmpty(page.getStatus())) {
            lambdaQueryWrapper.eq(MtRaffleType::getStatus, page.getStatus());
        }
        if (page.getMerchantId() != null && page.getMerchantId() > 0) {
            lambdaQueryWrapper.eq(MtRaffleType::getMerchantId, page.getMerchantId());
        }
        if (page.getStoreId() != null) {
            lambdaQueryWrapper.and(wq -> wq
                    .eq(MtRaffleType::getStoreId, 0)
                    .or()
                    .eq(MtRaffleType::getStoreId, page.getStoreId()));
        }

        lambdaQueryWrapper.orderByAsc(MtRaffleType::getSort);
        List<MtRaffleType> dataList = mtRaffleTypeMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(page.getPage(), page.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtRaffleType> paginationResponse = new PaginationResponse(pageImpl, MtRaffleType.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    @Override
    public MtRaffleType addRaffleType(MtRaffleType raffleType) throws BusinessCheckException {
        if (raffleType.getMerchantId() == null || raffleType.getMerchantId() <= 0) {
            throw new BusinessCheckException("新增抽奖类型失败：所属商户不能为空！");
        }
        if (StringUtil.isEmpty(raffleType.getName())) {
            throw new BusinessCheckException("抽奖类型名称不能为空！");
        }
        if (StringUtil.isEmpty(raffleType.getCode())) {
            throw new BusinessCheckException("抽奖类型编码不能为空！");
        }
        MtRaffleType entity = new MtRaffleType();
        entity.setMerchantId(raffleType.getMerchantId());
        entity.setStoreId(raffleType.getStoreId() == null ? 0 : raffleType.getStoreId());
        entity.setName(raffleType.getName());
        entity.setCode(raffleType.getCode());
        entity.setDescription(raffleType.getDescription());
        entity.setSort(raffleType.getSort() == null ? 0 : raffleType.getSort());
        entity.setOperator(raffleType.getOperator());
        entity.setStatus(StringUtil.isNotEmpty(raffleType.getStatus()) ? raffleType.getStatus() : StatusEnum.ENABLED.getKey());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        mtRaffleTypeMapper.insert(entity);
        return entity;
    }

    @Override
    public MtRaffleType getRaffleTypeById(Integer id) {
        return mtRaffleTypeMapper.selectById(id);
    }

    @Override
    public MtRaffleType updateRaffleType(MtRaffleType raffleType) throws BusinessCheckException {
        MtRaffleType entity = getRaffleTypeById(raffleType.getId());
        if (entity == null) {
            throw new BusinessCheckException("抽奖类型不存在");
        }
        if (raffleType.getName() != null) {
            entity.setName(raffleType.getName());
        }
        if (raffleType.getCode() != null) {
            entity.setCode(raffleType.getCode());
        }
        if (raffleType.getStoreId() != null) {
            entity.setStoreId(raffleType.getStoreId());
        }
        if (raffleType.getDescription() != null) {
            entity.setDescription(raffleType.getDescription());
        }
        if (raffleType.getOperator() != null) {
            entity.setOperator(raffleType.getOperator());
        }
        if (raffleType.getStatus() != null) {
            entity.setStatus(raffleType.getStatus());
        }
        if (raffleType.getSort() != null) {
            entity.setSort(raffleType.getSort());
        }
        entity.setUpdateTime(new Date());
        mtRaffleTypeMapper.updateById(entity);
        return entity;
    }

    @Override
    public List<MtRaffleType> getAvailableRaffleType(Integer merchantId, Integer storeId) {
        LambdaQueryWrapper<MtRaffleType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(MtRaffleType::getStatus, StatusEnum.ENABLED.getKey());
        if (merchantId != null && merchantId > 0) {
            lambdaQueryWrapper.eq(MtRaffleType::getMerchantId, merchantId);
        }
        if (storeId != null && storeId > 0) {
            lambdaQueryWrapper.and(wq -> wq.eq(MtRaffleType::getStoreId, 0).or().eq(MtRaffleType::getStoreId, storeId));
        }
        lambdaQueryWrapper.orderByAsc(MtRaffleType::getSort);
        return mtRaffleTypeMapper.selectList(lambdaQueryWrapper);
    }
}
