package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallConfig;
import com.ruoyi.mall.domain.MallOrderGoods;
import com.ruoyi.mall.domain.MallOrderOrder;
import com.ruoyi.mall.mapper.MallConfigMapper;
import com.ruoyi.mall.mapper.MallOrderGoodsMapper;
import com.ruoyi.mall.mapper.MallOrderOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSalesmanAchievementMapper;
import com.ruoyi.mall.domain.MallSalesmanAchievement;
import com.ruoyi.mall.service.IMallSalesmanAchievementService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 业务员业绩Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@Service
public class MallSalesmanAchievementServiceImpl implements IMallSalesmanAchievementService
{
    @Resource
    private MallSalesmanAchievementMapper mallSalesmanAchievementMapper;
    @Resource
    private MallOrderOrderMapper mallOrderOrderMapper;
    @Resource
    private MallConfigMapper mallConfigMapper;
    @Resource
    private MallOrderGoodsMapper mallOrderGoodsMapper;

    /**
     * 查询业务员业绩
     *
     * @param achievementId 业务员业绩主键
     * @return 业务员业绩
     */
    @Override
    public MallSalesmanAchievement selectMallSalesmanAchievementByAchievementId(Long achievementId)
    {
        return mallSalesmanAchievementMapper.selectMallSalesmanAchievementByAchievementId(achievementId);
    }

    /**
     * 查询业务员业绩列表
     *
     * @param mallSalesmanAchievement 业务员业绩
     * @return 业务员业绩
     */
    @Override
    public List<MallSalesmanAchievement> selectMallSalesmanAchievementList(MallSalesmanAchievement mallSalesmanAchievement)
    {
        return mallSalesmanAchievementMapper.selectMallSalesmanAchievementList(mallSalesmanAchievement);
    }

    /**
     * 新增业务员业绩
     *
     * @param mallSalesmanAchievement 业务员业绩
     * @return 结果
     */
    @Override
    public int insertMallSalesmanAchievement(MallSalesmanAchievement mallSalesmanAchievement)
    {
        return mallSalesmanAchievementMapper.insertMallSalesmanAchievement(mallSalesmanAchievement);
    }

    /**
     * 修改业务员业绩
     *
     * @param mallSalesmanAchievement 业务员业绩
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateMallSalesmanAchievement(MallSalesmanAchievement mallSalesmanAchievement)
    {
        List<Long> achievementIdList = mallSalesmanAchievement.getAchievementIdList();
        if (achievementIdList != null && !achievementIdList.isEmpty()){
            return mallSalesmanAchievementMapper.updateMallSalesmanAchievementBatch(mallSalesmanAchievement);
        }else {
            return mallSalesmanAchievementMapper.updateMallSalesmanAchievement(mallSalesmanAchievement);
        }
    }

    /**
     * 批量删除业务员业绩
     *
     * @param achievementIds 需要删除的业务员业绩主键
     * @return 结果
     */
    @Override
    public int deleteMallSalesmanAchievementByAchievementIds(Long[] achievementIds)
    {
        return mallSalesmanAchievementMapper.deleteMallSalesmanAchievementByAchievementIds(achievementIds);
    }

    /**
     * 删除业务员业绩信息
     *
     * @param achievementId 业务员业绩主键
     * @return 结果
     */
    @Override
    public int deleteMallSalesmanAchievementByAchievementId(Long achievementId)
    {
        return mallSalesmanAchievementMapper.deleteMallSalesmanAchievementByAchievementId(achievementId);
    }

    /**
     * 新增或修改业务员业绩
     *
     * @param orderId 订单ID
     * @param status 状态：0-冻结中，1-已获得，2-已失效
     * @return 结果
     */
    @Override
    public int insertOrUpdateMallSalesmanAchievement(Long orderId, Integer status) {
        MallOrderOrder mallOrderOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
        // MallConfig money = mallConfigMapper.selectMallConfigByIdentifying("money");
        // Integer configInt = money.getConfigInt();

        // BigDecimal multiply = new BigDecimal("0.01").multiply(BigDecimal.valueOf(configInt));

        BigDecimal commissionByOrderId = mallOrderGoodsMapper.selectOrderGoodsCommissionByOrderId(orderId);

        MallSalesmanAchievement salesmanAchievement = new MallSalesmanAchievement();
        salesmanAchievement.setOrderStatus(mallOrderOrder.getStatus());
        salesmanAchievement.setStatus(status);
        if (status == 0) {
            if (commissionByOrderId != null && commissionByOrderId.compareTo(new BigDecimal(0)) > 0) {
                salesmanAchievement.setSalesmanId(mallOrderOrder.getSalesmanId().longValue());
                salesmanAchievement.setPrice(mallOrderOrder.getOrderPrice());
                salesmanAchievement.setMoney(commissionByOrderId.setScale(2, RoundingMode.HALF_UP));
                salesmanAchievement.setLinkOrderId(mallOrderOrder.getId());
                salesmanAchievement.setLinkOrderNo(mallOrderOrder.getOrderNo());
                salesmanAchievement.setDeliveryType(mallOrderOrder.getDeliveryType());
                salesmanAchievement.setLinkUserId(mallOrderOrder.getUserId());
                salesmanAchievement.setCreateAt(mallOrderOrder.getCreateAt());
                mallSalesmanAchievementMapper.insertMallSalesmanAchievement(salesmanAchievement);
            }
        } else {
            MallSalesmanAchievement achievement = mallSalesmanAchievementMapper.selectMallSalesmanAchievementByOrderId(orderId);
            salesmanAchievement.setAchievementId(achievement.getAchievementId());
            salesmanAchievement.setUpdateAt(DateUtils.getTime());
            mallSalesmanAchievementMapper.updateMallSalesmanAchievement(salesmanAchievement);
        }
        return 1;
    }

    /**
     * 业务员管理-收益统计
     *
     * @param params 参数
     * @return 收益统计
     */
    @Override
    public Map<String, Object> getSalesmanRevenueStatistics(Map<String, Object> params) {
        HashMap<String, Object> map = new HashMap<>();
        params.put("status", 0);
        map.put("freezePrice", mallSalesmanAchievementMapper.getSalesmanRevenueStatistics(params));
        params.put("status", 1);
        map.put("alreadyPrice", mallSalesmanAchievementMapper.getSalesmanRevenueStatistics(params));

        params.put("status", "('0')");// 待付款
        map.put("waitPayOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));
        params.put("status", "('10')");// 待拼团
        map.put("waitCombineOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));
        params.put("status", "('1')");// 待配送
        map.put("waitDeliverOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));
        params.put("status", "('2')");// 待收货
        map.put("waitInOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));
        params.put("status", "('4')");// 已完成
        map.put("completeOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));
        params.put("status", "('5','6')");// 退货
        map.put("refundOrderNum", mallSalesmanAchievementMapper.getSalesmanRevenueOrderStatistics(params));

        return map;
    }

    /**
     * 业务员管理-订单列表
     *
     * @param params 参数
     * @return 收益统计
     */
    @Override
    public List<Map<String, Object>> getSalesmanStatisticsOrderList(Map<String, Object> params) {
        List<Map<String, Object>> statisticsOrderList = mallSalesmanAchievementMapper.getSalesmanStatisticsOrderList(params);
        if (statisticsOrderList.size() > 0) {
            for (Map<String, Object> stringObjectMap : statisticsOrderList) {
                MallOrderGoods mallOrderGoods = new MallOrderGoods();
                mallOrderGoods.setOrderId(Long.valueOf(stringObjectMap.get("orderId").toString()));
                stringObjectMap.put("orderGoodsList", mallOrderGoodsMapper.selectMallOrderGoodsList(mallOrderGoods));
            }
        }
        return statisticsOrderList;
    }

    /**
     * 业务员管理-业绩排行榜
     *
     * @param params 参数
     * @return 业绩排行榜
     */
    @Override
    public List<Map<String, Object>> getSalesmanStatisticsRankingList(Map<String, Object> params) {
        return mallSalesmanAchievementMapper.getSalesmanStatisticsRankingList(params);
    }
}
