package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.mall.mapper.MallMemberIntegralMapper;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.mapper.MallOrderOrderMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallCouponMapper;
import com.ruoyi.mall.domain.MallCoupon;
import com.ruoyi.mall.service.IMallCouponService;

import javax.annotation.Resource;

/**
 * 优惠券Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-17
 */
@Service
public class MallCouponServiceImpl implements IMallCouponService
{
    @Resource
    private MallCouponMapper mallCouponMapper;
    @Resource
    private MallOrderOrderMapper mallOrderOrderMapper;
    @Resource
    private MallMemberIntegralMapper mallMemberIntegralMapper;
    @Resource
    private MallMemberUserMapper mallMemberUserMapper;

    /**
     * 查询优惠券
     *
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public MallCoupon selectMallCouponById(Long id)
    {
        return mallCouponMapper.selectMallCouponById(id);
    }

    /**
     * 查询优惠券列表
     *
     * @param mallCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<MallCoupon> selectMallCouponList(MallCoupon mallCoupon)
    {
        return mallCouponMapper.selectMallCouponList(mallCoupon);
    }

    /**
     * 新增优惠券
     *
     * @param mallCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertMallCoupon(MallCoupon mallCoupon)
    {
        return mallCouponMapper.insertMallCoupon(mallCoupon);
    }

    /**
     * 修改优惠券
     *
     * @param mallCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateMallCoupon(MallCoupon mallCoupon)
    {
        return mallCouponMapper.updateMallCoupon(mallCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteMallCouponByIds(Long[] ids)
    {
        return mallCouponMapper.deleteMallCouponByIds(ids);
    }

    /**
     * 删除优惠券信息
     *
     * @param id 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteMallCouponById(Long id)
    {
        return mallCouponMapper.deleteMallCouponById(id);
    }

    @Override
    public List<Map<String, Object>> selectMallCouponVOList(Integer type) {
        return mallCouponMapper.selectMallCouponVOList(type);
    }

    /**
     * 兑换优惠券列表
     *
     * @param params userId 用户id
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> exchangeCouponList(Map<String, Object> params) {
        // // 新增积分记录
        // MallOrderOrder mallOrderOrder = new MallOrderOrder();
        // mallOrderOrder.setUserId(Long.valueOf(params.get("userId").toString()));
        // mallOrderOrder.setStatus("4");
        // List<MallOrderOrder> orderOrderList = mallOrderOrderMapper.selectMallOrderOrderList(mallOrderOrder);
        //
        // if (orderOrderList.size() > 0) {
        //     List<MallMemberIntegral> memberIntegralList = new ArrayList<>();
        //     for (MallOrderOrder orderOrder : orderOrderList) {
        //         MallMemberIntegral memberIntegral = new MallMemberIntegral();
        //         memberIntegral.setUserId(orderOrder.getUserId());
        //         memberIntegral.setIntegral(orderOrder.getPayment().setScale(0, RoundingMode.DOWN));
        //         memberIntegral.setMark(0);
        //         memberIntegral.setType(0);
        //         memberIntegral.setSendType(0);
        //         memberIntegral.setSendId(orderOrder.getUserId());
        //         memberIntegral.setResId(orderOrder.getOrderId());
        //         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //         memberIntegral.setCreateAt(formatter.parse(orderOrder.getCreateAt()));
        //         memberIntegralList.add(memberIntegral);
        //
        //     }
        //     mallMemberIntegralMapper.bachInsertMallMemberIntegral(memberIntegralList);
        // }
        List<Map<String, Object>> mapList = mallCouponMapper.exchangeCouponList(params);
        if (mapList.size() > 0) {
            Map<String, Object> userIntegral = this.userIntegral(params);
            if (!userIntegral.isEmpty()) {
                // 用户可用积分
                BigDecimal userCanIntegral = new BigDecimal(userIntegral.get("userCanIntegral").toString());
                for (Map<String, Object> stringObjectMap : mapList) {
                    // 优惠券兑换积分
                    BigDecimal integral = new BigDecimal(stringObjectMap.get("integral").toString());
                    String type = stringObjectMap.get("type").toString();
                    String isExchange = stringObjectMap.get("isExchange").toString();
                    if ("1".equals(type) && "1".equals(isExchange)) {
                        if (integral.compareTo(userCanIntegral) > 0) {
                            stringObjectMap.put("isExchange", 0);
                        }

                    }
                }
            }
        }

        return mapList;
    }

    /**
     * 用户积分
     *
     * @param params userId 用户id
     * @return 结果
     */
    @Override
    public Map<String, Object> userIntegral(Map<String, Object> params) {
        // 用户用掉的积分
        BigDecimal userIntegral = new BigDecimal("0");
        userIntegral = mallMemberIntegralMapper.selectMallMemberIntegralByUserId(Long.valueOf(params.get("userId").toString()), 1);
        userIntegral = userIntegral.setScale(0, RoundingMode.DOWN);

        // 用户获得的积分
        BigDecimal userAllIntegral = new BigDecimal("0");
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", params.get("userId").toString());
        userAllIntegral = mallMemberUserMapper.getUserAllIntegral(map);
        userAllIntegral = userAllIntegral.setScale(0, RoundingMode.DOWN);

        // 用户可用积分
        BigDecimal userCanIntegral = userAllIntegral.subtract(userIntegral);

        params.put("userCanIntegral", userCanIntegral);
        return params;
    }
}
