package com.hai.service.impl;

import com.alibaba.fastjson.JSON;
import com.hai.common.Base64Util;
import com.hai.common.QRCodeGenerator;
import com.hai.common.security.AESEncodeUtil;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.IDGenerator;
import com.hai.dao.HighDiscountAgentRelMapper;
import com.hai.entity.*;
import com.hai.service.HighAgentService;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountAgentRelService;
import com.hai.service.HighDiscountService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/4 00:15
 */
@Service("highDiscountAgentRelService")
public class HighDiscountAgentRelServiceImpl implements HighDiscountAgentRelService {

    @Resource
    private HighDiscountAgentRelMapper highDiscountAgentRelMapper;

    @Resource
    private HighAgentService highAgentService;

    @Resource
    private HighDiscountService highDiscountService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void insertDiscountAgentRel(HighDiscountAgentRel highDiscountAgentRel,Integer stockCount) throws Exception {
        if (highDiscountAgentRel.getId() != null) {
            highDiscountAgentRelMapper.updateByPrimaryKey(highDiscountAgentRel);
        } else {
            highDiscountAgentRelMapper.insert(highDiscountAgentRel);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("type", "DISCOUNT");

        HighDiscountAgentCode code;
        String path = "/home/project/hsg/filesystem/agentQrCode/";
        String folder = highDiscountAgentRel.getAgentId() + "/" + highDiscountAgentRel.getDiscountId() + "/";

        for (int i = 0;i < stockCount;i++) {
            code = new HighDiscountAgentCode();
            code.setDiscountAgentId(highDiscountAgentRel.getId());
            code.setStatus(1);
            code.setCreateTime(new Date());
            highDiscountAgentCodeService.insertCode(code);
            // 二维码参数
            map.put("id", code.getId());
            String qrCodeImg = DateUtil.date2String(new Date(),highDiscountAgentRel.getId()+"yyyyMMddHHmmss") + IDGenerator.nextId(10) +".png";

            // 生成二维码
            String param = "https://hsg.dctpay.com/wx/?action=gogogo&id=" + Base64Util.encode(AESEncodeUtil.aesEncrypt(JSON.toJSONString(map)));
            QRCodeGenerator.generateQRCodeImage(param, 350, 350, path + folder + qrCodeImg);
            code.setQrCode(qrCodeImg);
            code.setExt1(param);
            highDiscountAgentCodeService.updateCode(code);
        }
    }

    @Override
    public void updateDiscountAgentRel(HighDiscountAgentRel highDiscountAgentRel) {
        highDiscountAgentRelMapper.updateByPrimaryKey(highDiscountAgentRel);
    }

    @Override
    public void deleteDiscountAgentRel(Long id) {
        highDiscountAgentRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public HighDiscountAgentRel getRelByDiscountAgent(Long discountId, Long agentId) {
        HighDiscountAgentRelExample example = new HighDiscountAgentRelExample();
        example.createCriteria().andDiscountIdEqualTo(discountId).andAgentIdEqualTo(agentId);
        List<HighDiscountAgentRel> list = highDiscountAgentRelMapper.selectByExample(example);
        if (list != null && list.size() >0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighDiscountAgentRel> getRelByDiscountId(Long discountId) {
        HighDiscountAgentRelExample example = new HighDiscountAgentRelExample();
        example.createCriteria().andDiscountIdEqualTo(discountId);
        List<HighDiscountAgentRel> list = highDiscountAgentRelMapper.selectByExample(example);
        return list;
    }

    @Override
    public HighDiscountAgentRel getRelById(Long id) {
        HighDiscountAgentRel rel = highDiscountAgentRelMapper.selectByPrimaryKey(id);
        if (rel != null) {
            rel.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId()));
            rel.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId()));
        }
        return rel;
    }

    @Override
    public List<HighDiscountAgentRel> getRelList(Map<String, Object> map) {
        HighDiscountAgentRelExample example = new HighDiscountAgentRelExample();
        HighDiscountAgentRelExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "discountId") != null) {
            criteria.andDiscountIdEqualTo(MapUtils.getLong(map, "discountId"));
        }

        if (MapUtils.getLong(map, "agentId") != null) {
            criteria.andAgentIdEqualTo(MapUtils.getLong(map, "agentId"));
        }

        criteria.andStatusEqualTo(1);
        example.setOrderByClause("create_time desc");
        List<HighDiscountAgentRel> list = highDiscountAgentRelMapper.selectByExample(example);
        for (HighDiscountAgentRel rel : list) {
            rel.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId()));
            rel.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId()));
            // rel.setTotalCount(highDiscountAgentCodeService.getTotalDiscount(rel.getId()));
            // rel.setSalesCount(highDiscountAgentCodeService.getSalesDiscount(rel.getId()));
        }
        return list;
    }
}
