package com.iformall.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;

import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.WxCouponCVo;
import com.iformall.enums.EnumCouponChannelType;
import com.iformall.enums.EnumScreenAdType;
import com.iformall.mapper.WxCampaignMapper;
import com.iformall.mapper.WxCouponChannelMapper;
import com.iformall.mapper.WxScreenAdMapper;
import com.iformall.service.WxAppinfoService;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxScreenAdService;
import com.iformall.service.wechat.FmOpenService;
import com.iformall.utils.MaUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WxScreenAdServiceImpl implements WxScreenAdService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    WxScreenAdMapper wxScreenAdMapper;

    @Autowired
    WxAppinfoService wxAppinfoService;

    @Autowired
    WxCouponChannelMapper wxCouponChannelMapper;

    @Autowired
    WxMerchantService wxMerchantService;

    @Autowired
    WxCampaignMapper wxCampaignMapper;

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

    @Override
    public WxScreenAd getById(Long id) {
        return wxScreenAdMapper.selectById(id);
    }

    @Override
    public ResultData saveOrUpdate(WxScreenAd record) {
        if (record.getId() == null) {
            if (record.getType() == null) {
                return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
            }
            if (record.getType().equals(EnumScreenAdType.COUPON.getCode())) {
                if (record.getTargetId() == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                WxCouponCVo wxCouponCVo = wxCouponChannelMapper.findVoDetail(record.getTargetId());
                if (wxCouponCVo == null)
                    return  new ResultData(ErrorCode.COUPON_ORDER_IS_NULL);
                record.setCoverImg(wxCouponCVo.getCoverImg());
                record.setSubType(wxCouponCVo.getTargetAd());
                record.setTitle(wxCouponCVo.getTitle());

            } else if (record.getType().equals(EnumScreenAdType.CAMPAIGN.getCode())){
                if (record.getTargetId() == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                WxCampaign wxCampaign = wxCampaignMapper.selectById(record.getTargetId());
                if (wxCampaign == null)
                    return  new ResultData(ErrorCode.COUPON_ORDER_IS_NULL);
                record.setCoverImg(wxCampaign.getCoverImg());
                record.setTitle(wxCampaign.getTitle());
            } else if (record.getType().equals(EnumScreenAdType.PICTURE.getCode())) {
                if (record.getCoverImg() == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
            } else if (record.getType().equals(EnumScreenAdType.MERCHANT.getCode())) {
                if (record.getExtInfo() == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                JSONObject jo = JSONObject.parseObject(record.getExtInfo());
                if (jo == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                if (jo.getInteger("executionIndex") == null || jo.getInteger("populationIndex") == null)
                    return  new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                WxMerchant wxMerchant = wxMerchantService.getById(record.getTargetId());
                if (wxMerchant == null)
                    return  new ResultData(ErrorCode.COUPON_ORDER_IS_NULL);
                record.setCoverImg(wxMerchant.getImgUrl());
                record.setTitle(wxMerchant.getName());
            }

            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
        	record.setCreateDate(new Date());
            record.setUpdateDate(new Date());
            wxScreenAdMapper.insert(record);

        } else {
            record.setUpdateDate(new Date());
            wxScreenAdMapper.updateById(record);
        }
        return new ResultData();
    }

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

    @Override
    public List<WxScreenAd> findList(WxScreenAd wxTagsType) {
        return wxScreenAdMapper.findList(wxTagsType);
    }


    @Override
    public void updateStatusByCouponChannelId(Long couponChannelId, String tenantId, int status) {
        WxScreenAd wxScreenAd = new WxScreenAd();
        wxScreenAd.setTenantId(tenantId);
        wxScreenAd.setStatus(status);
        wxScreenAd.setTargetId(couponChannelId);
        wxScreenAd.setUpdateDate(new Date());
        wxScreenAdMapper.updateStatusByCouponChannelId(wxScreenAd);
    }

    @Override
    public void updateStatusByCouponId(Long couponId, String tenantId, int status) {

        WxCouponChannel wxCouponChannel = new WxCouponChannel();
        wxCouponChannel.setTenantId(tenantId);
        wxCouponChannel.setCouponId(couponId);

        List<WxCouponChannel> list = wxCouponChannelMapper.findList(wxCouponChannel);

        list.stream().forEach(cc->{
            WxScreenAd wxScreenAd = new WxScreenAd();
            wxScreenAd.setTenantId(tenantId);
            wxScreenAd.setStatus(status);
            wxScreenAd.setTargetId(cc.getId());
            wxScreenAd.setUpdateDate(new Date());
            wxScreenAdMapper.updateStatusByCouponChannelId(wxScreenAd);
        });
    }
}
