package com.hai.service.impl;

import com.hai.common.utils.DateUtil;
import com.hai.dao.HighGoodsPriceReferMapper;
import com.hai.entity.*;
import com.hai.enum_type.ApproveType;
import com.hai.service.HighApproveService;
import com.hai.service.HighCouponService;
import com.hai.service.HighGoodsPriceReferService;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description: 产品销售价格配置
 * @Date: 2021/3/14 16:26
 */
@Service("highGoodsPriceReferService")
public class HighGoodsPriceReferServiceImpl implements HighGoodsPriceReferService {

    @Resource
    private HighGoodsPriceReferMapper highGoodsPriceReferMapper;

    @Resource
    private HighApproveService highApproveService;

    @Resource
    private HighCouponService highCouponService;

    @Override
    public void insertPriceRefer(HighGoodsPriceRefer highGoodsPriceRefer) {
        highGoodsPriceReferMapper.insert(highGoodsPriceRefer);
    }

    @Override
    public void updatePriceRefer(HighGoodsPriceRefer highGoodsPriceRefer) {
        highGoodsPriceReferMapper.updateByPrimaryKey(highGoodsPriceRefer);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void submitApprove(HighGoodsPriceRefer highGoodsPriceRefer) throws Exception {
        insertPriceRefer(highGoodsPriceRefer);

        HighApprove approve = new HighApprove();
        approve.setObjectType(ApproveType.UP_SHELF_APPROVE.getType());
        approve.setObjectId(highGoodsPriceRefer.getId());
        approve.setObjectName(highCouponService.getCouponById(highGoodsPriceRefer.getObjectId()).getCouponName());
        approve.setApproveSerialNo(DateUtil.date2String(new Date(), "yyyyMMddHHmmss"));
        approve.setStatus(1);
        approve.setCreateTime(new Date());
        approve.setUpdateTime(new Date());
        approve.setSubmitOperatorId(highGoodsPriceRefer.getOperatorId());
        approve.setSubmitOperatorName(highGoodsPriceRefer.getOperatorName());
        highApproveService.insertApprove(approve);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void submitApproveList(List<HighGoodsPriceRefer> highGoodsPriceReferList) throws Exception {
        for (HighGoodsPriceRefer highGoodsPriceRefer : highGoodsPriceReferList) {
            submitApprove(highGoodsPriceRefer);
        }
    }


    @Override
    public HighGoodsPriceRefer getPriceReferById(Long id) {
        return highGoodsPriceReferMapper.selectByPrimaryKey(id);
    }

    @Override
    public void couponAllSalesPriceInvalid(Long couponId) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        statusList.add(101);
        HighGoodsPriceReferExample example = new HighGoodsPriceReferExample();
        example.createCriteria()
                .andObjectTypeEqualTo(1)
                .andObjectIdEqualTo(couponId)
                .andPriceTypeEqualTo(1)
                .andStatusIn(statusList);

        List<HighGoodsPriceRefer> referList = highGoodsPriceReferMapper.selectByExample(example);
        if (referList != null && referList.size() > 0) {
            for (HighGoodsPriceRefer refer : referList) {
                refer.setStatus(4);
                highGoodsPriceReferMapper.updateByPrimaryKey(refer);
            }
        }
    }

    @Override
    public void couponAllDiscountPriceInvalid(Long couponId) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        statusList.add(101);
        HighGoodsPriceReferExample example = new HighGoodsPriceReferExample();
        example.createCriteria()
                .andObjectTypeEqualTo(1)
                .andObjectIdEqualTo(couponId)
                .andPriceTypeEqualTo(2)
                .andStatusIn(statusList);

        List<HighGoodsPriceRefer> referList = highGoodsPriceReferMapper.selectByExample(example);
        if (referList != null && referList.size() > 0) {
            for (HighGoodsPriceRefer refer : referList) {
                refer.setStatus(4);
                highGoodsPriceReferMapper.updateByPrimaryKey(refer);
            }
        }
    }

    @Override
    public void couponAllInvalidKeepCurrent(Long couponId) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(101);

        HighGoodsPriceReferExample example = new HighGoodsPriceReferExample();
        example.createCriteria().andObjectIdEqualTo(couponId).andStatusIn(statusList);

        List<HighGoodsPriceRefer> referList = highGoodsPriceReferMapper.selectByExample(example);
        if (referList != null && referList.size() > 0) {
            for (HighGoodsPriceRefer refer : referList) {
                refer.setStatus(4);
                highGoodsPriceReferMapper.updateByPrimaryKey(refer);
            }
        }
    }

    @Override
    public List<HighGoodsPriceRefer> getPriceList(Map<String, Object> map) {
        HighGoodsPriceReferExample example = new HighGoodsPriceReferExample();
        HighGoodsPriceReferExample.Criteria criteria = example.createCriteria();

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

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

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

        criteria.andStatusNotEqualTo(0);
        return highGoodsPriceReferMapper.selectByExample(example);
    }

    @Override
    public Boolean isAddCondition(Integer objectType, Long objectId, Integer priceType) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(101);

        HighGoodsPriceReferExample example = new HighGoodsPriceReferExample();
        example.createCriteria()
                .andObjectTypeEqualTo(objectType)
                .andObjectIdEqualTo(objectId)
                .andPriceTypeEqualTo(objectType)
                .andStatusIn(statusList);

        List<HighGoodsPriceRefer> list = highGoodsPriceReferMapper.selectByExample(example);
        if (list.size() > 0) {
            return  false;
        }
        return true;
    }


}
