package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.MemCouponFromDsp;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.base.TenantEntity;
import com.iformall.enums.EnumCouponDspStatus;
import com.iformall.enums.EnumUserType;
import com.iformall.mapper.MemCouponFromDspMapper;
import com.iformall.mapper.WxCouponMapper;
import com.iformall.service.MemCouponFromDspService;
import com.iformall.service.WxCUserService;
import com.iformall.service.WxOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

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

@Service
public class MemCouponFromDspServiceImpl implements MemCouponFromDspService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    private MemCouponFromDspMapper memCouponFromDspMapper;

	@Autowired
    private WxCouponMapper couponMapper;

    @Autowired
    private WxOrderService wxOrderService;
    @Autowired
    private WxCUserService wxCUserService;


    @Override
    public PageInfo<MemCouponFromDsp> listAsPage(MemCouponFromDsp record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> memCouponFromDspMapper.findList(record));
    }

    @Override
    public List<MemCouponFromDsp> getList(MemCouponFromDsp record) {
        return memCouponFromDspMapper.findList(record);
    }

    @Override
    public MemCouponFromDsp getById(Long id) {
        return memCouponFromDspMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(MemCouponFromDsp record) {
        Date curDate = new Date();
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
        	record.setCreateDate(curDate);
        	record.setUpdateDate(curDate);
            memCouponFromDspMapper.insert(record);
        } else {
            record.setUpdateDate(curDate);
            memCouponFromDspMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        memCouponFromDspMapper.deleteById(id);
    }

    @Override
    public Integer countByInfo(MemCouponFromDsp record) {
        return memCouponFromDspMapper.selectCount(new QueryWrapper<>(record));
    }

    @Override
    public Integer couponOrder(MemCouponFromDsp record) {
        // 获取C端用户
        WxCUser user = wxCUserService.getByObject(new WxCUser() {{
            updateTenantInfo(record);
            setPhone(record.getPhone());
        }});
        if (user == null) {
            return -1;
        }
        //用于标记当前操作人为A端用户
        user.setOperatorType(EnumUserType.MALLUSER.getCode());
        user.setOperatorId(user.getId());
        // 手机是否有未注的券
        MemCouponFromDsp memQ = new MemCouponFromDsp() {{
            updateTenantInfo(record);
            setPhone(record.getPhone());
            setStatus(EnumCouponDspStatus.RECEIVED.getCode());
        }};
        List<MemCouponFromDsp> couponFromDsps = memCouponFromDspMapper.findList(memQ);
        for (MemCouponFromDsp memCouponFromDsp: couponFromDsps) {
            // 注入券码, 无论此券是有价还是免费
            WxCoupon coupon = couponMapper.selectById(memCouponFromDsp.getCouponId());
            if (coupon != null) {
                try {
                    Long couponOrderId = wxOrderService.saveOuterOrderForCoupon(user, coupon, memCouponFromDsp.getCouponChannelId(), null, null);
                    if (couponOrderId != null) {
                        MemCouponFromDsp updateRecord = new MemCouponFromDsp();
                        updateRecord.updateTenantInfo(memCouponFromDsp);
                        updateRecord.setId(memCouponFromDsp.getId());
                        updateRecord.setCouponOrderId(couponOrderId);
                        updateRecord.setStatus(EnumCouponDspStatus.SENT_CO.getCode());
                        updateRecord.setUpdateDate(new Date());
                        memCouponFromDspMapper.updateById(updateRecord);
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    continue;
                }

            }
        }
        return 0;
    }

    @Override
    public void couponOrderFromDsp(TenantEntity tenantEntity, String phone) {
        List<MemCouponFromDsp> couponFromDsps = getList( new MemCouponFromDsp() {{
            updateTenantInfo(tenantEntity);
            setPhone(phone);
        }});
        for(MemCouponFromDsp memCouponFromDsp: couponFromDsps ){
            couponOrder(memCouponFromDsp);
        }
    }
    
    
    
    
    
    
}
