package com.hai.service.impl;

import com.hai.common.utils.DateUtil;
import com.hai.dao.HighCouponCodeMapper;
import com.hai.dao.HighDiscountAgentCodeMapper;
import com.hai.entity.*;
import com.hai.service.*;
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.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/14 22:09
 */
@Service("highDiscountAgentCodeService")
public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeService {

    @Resource
    private HighDiscountAgentCodeMapper highDiscountAgentCodeMapper;

    @Resource
    private HighDiscountAgentRelService highDiscountAgentRelService;

    @Resource
    private HighCouponCodeMapper highCouponCodeMapper;

    @Resource
    private HighDiscountService highDiscountService;

    @Resource
    private HighAgentService highAgentService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighUserService highUserService;

    @Resource
    private HighDiscountUserRelService highDiscountUserRelService;

    @Override
    public void insertCodeList(List<HighDiscountAgentCode> discountAgentCodeList) {
        highDiscountAgentCodeMapper.insertListCode(discountAgentCodeList);
    }

    @Override
    public void insertCode(HighDiscountAgentCode highDiscountAgentCode) {
        highDiscountAgentCodeMapper.insert(highDiscountAgentCode);
    }

    @Override
    public void updateCode(HighDiscountAgentCode highDiscountAgentCode) {
        highDiscountAgentCodeMapper.updateByPrimaryKey(highDiscountAgentCode);
    }

    @Override
    public List<HighDiscountAgentCode> getDiscountCode(Map<String, Object> map) {
        HighDiscountAgentCodeExample example = new HighDiscountAgentCodeExample();
        HighDiscountAgentCodeExample.Criteria criteria = example.createCriteria();

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

        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        }

        example.setOrderByClause("create_time desc");
        List<HighDiscountAgentCode> list = highDiscountAgentCodeMapper.selectByExample(example);
        for (HighDiscountAgentCode code : list) {
            HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(code.getDiscountAgentId());
            if (rel != null) {
                code.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId()));
                code.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId()));
            }

            HighDiscountUserRel userRel = highDiscountUserRelService.getRelByDiscount(code.getId());
            if (userRel != null) {
                code.setHighUser(highUserService.findByUserId(userRel.getUserId()));
                code.setHighCouponCode(highCouponCodeMapper.selectCouponCodeByUserDiscount(userRel.getId()));
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getDiscountUseConditionList(Map<String, Object> map) {
        try {
            if (MapUtils.getLong(map, "receiveTimeS") != null) {
               map.put("receiveTimeS", DateUtil.date2String(new Date(MapUtils.getLong(map, "receiveTimeS")), "yyyy-MM-dd HH:mm:ss"));
            }
            if (MapUtils.getLong(map, "receiveTimeE") != null) {
                map.put("receiveTimeE", DateUtil.date2String(new Date(MapUtils.getLong(map, "receiveTimeE")), "yyyy-MM-dd HH:mm:ss"));
            }

            if (MapUtils.getLong(map, "useTimeS") != null) {
                map.put("useTimeS", DateUtil.date2String(new Date(MapUtils.getLong(map, "useTimeS")), "yyyy-MM-dd HH:mm:ss"));
            }
            if (MapUtils.getLong(map, "useTimeE") != null) {
                map.put("useTimeE", DateUtil.date2String(new Date(MapUtils.getLong(map, "useTimeE")), "yyyy-MM-dd HH:mm:ss"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return highDiscountAgentCodeMapper.getDiscountUseConditionList(map);
    }

    @Override
    public List<HighDiscountAgentCode> getDiscountCodeByDiscountAgentId(Long discountAgentId) {

        HighDiscountAgentCodeExample example = new HighDiscountAgentCodeExample();
        HighDiscountAgentCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDiscountAgentIdEqualTo(discountAgentId).andStatusEqualTo(5);
        example.setOrderByClause("create_time desc");
        return highDiscountAgentCodeMapper.selectByExample(example);
    }

    @Override
    public List<HighDiscountAgentCode> getDiscountCodeByStatus(Long discountAgentId, Integer status) {
        HighDiscountAgentCodeExample example = new HighDiscountAgentCodeExample();
        HighDiscountAgentCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDiscountAgentIdEqualTo(discountAgentId).andStatusEqualTo(status);
        example.setOrderByClause("create_time desc");
        return highDiscountAgentCodeMapper.selectByExample(example);
    }

    @Override
    public HighDiscountAgentCode getCodeById(Long id) {
        return highDiscountAgentCodeMapper.selectByPrimaryKey(id);
    }


    @Override
    public Integer getTotalDiscount(Long discountAgentId) {
        HighDiscountAgentCodeExample example = new HighDiscountAgentCodeExample();
        example.createCriteria().andDiscountAgentIdEqualTo(discountAgentId);
        return highDiscountAgentCodeMapper.selectByExample(example).size();
    }

    @Override
    public Integer getSalesDiscount(Long discountAgentId) {
        HighDiscountAgentCodeExample example = new HighDiscountAgentCodeExample();
        example.createCriteria().andDiscountAgentIdEqualTo(discountAgentId).andStatusNotEqualTo(1);
        return highDiscountAgentCodeMapper.selectByExample(example).size();
    }

    @Override
    public List<HighDiscountAgentCode> getDiscountAgentCodByLimit(Long discountAgentId, Long num) {
        return highDiscountAgentCodeMapper.getDiscountAgentCodByLimit(discountAgentId,num);
    }
}
