package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.HighApproveMapper;
import com.hai.entity.HighApprove;
import com.hai.entity.HighApproveExample;
import com.hai.entity.HighCoupon;
import com.hai.enum_type.ApproveType;
import com.hai.model.HighCouponModel;
import com.hai.service.HighApproveService;
import com.hai.service.HighCouponService;
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;
import java.util.Objects;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/14 20:47
 */
@Service("highApproveService")
public class HighApproveServiceImpl implements HighApproveService {

    @Resource
    private HighApproveMapper highApproveMapper;

    @Resource
    private HighCouponService highCouponService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void insertApprove(HighApprove highApprove) {

        // 卡卷上架审批
        if (Objects.equals(highApprove.getObjectType(), ApproveType.UP_SHELF_APPROVE.getType())) {
            highCouponService.upShelfApprove(highApprove.getObjectId());
        }

        // 卡卷价格审批
        if (Objects.equals(highApprove.getObjectType(), ApproveType.COUPON_PRICE_APPROVE.getType())) {
            // 暂时不需要调用接口
        }
        highApproveMapper.insert(highApprove);
    }

    @Override
    public void updateApprove(HighApprove highApprove) {
        highApproveMapper.updateByPrimaryKey(highApprove);
    }

    @Override
    public HighApprove getApproveById(Long id) {
        return highApproveMapper.selectByPrimaryKey(id);
    }

    @Override
    public void approveProcessed(HighApprove highApprove) {
        if (highApprove.getObjectType() == ApproveType.UP_SHELF_APPROVE.getType()) {
            highCouponService.upShelfApproveResult(highApprove);
        }

        if (highApprove.getObjectType() == ApproveType.COUPON_PRICE_APPROVE.getType()) {
            highCouponService.priceApproveResult(highApprove);
        }

        updateApprove(highApprove);
    }

    @Override
    public List<HighApprove> getApproveList(Map<String, Object> map) {
        HighApproveExample example = new HighApproveExample();
        HighApproveExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getString(map, "approveSerialNo") != null) {
            criteria.andApproveSerialNoEqualTo(MapUtils.getString(map, "approveSerialNo"));
        }

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

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

        if (MapUtils.getString(map, "objectName") != null) {
            criteria.andObjectNameLike("%" + MapUtils.getString(map, "objectName") + "%");
        }

        example.setOrderByClause("create_time desc");
        return highApproveMapper.selectByExample(example);
    }
}
