package com.ruoyi.buyerapi.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.ruoyi.buyerapi.domain.BuyerCompanyInfo;
import com.ruoyi.buyerapi.domain.BuyerGoods;
import com.ruoyi.buyerapi.mapper.BuyerCompanyInfoMapper;
import com.ruoyi.buyerapi.mapper.BuyerGoodsMapper;
import com.ruoyi.buyerapi.service.IBuyerCompanyInfoService;
import com.ruoyi.buyerapi.service.IBuyerGoodsService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerOrderRecordMapper;
import com.ruoyi.buyerapi.domain.BuyerOrderRecord;
import com.ruoyi.buyerapi.service.IBuyerOrderRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class BuyerOrderRecordServiceImpl implements IBuyerOrderRecordService {
    @Autowired
    private BuyerOrderRecordMapper buyerOrderRecordMapper;

    @Autowired
    private BuyerCompanyInfoMapper buyerCompanyInfoMapper;

    @Autowired
    private BuyerGoodsMapper buyerGoodsMapper;

    /**
     * 查询订单记录
     *
     * @param id 订单记录主键
     * @return 订单记录
     */
    @Override
    public BuyerOrderRecord selectBuyerOrderRecordById(String id) {
        BuyerOrderRecord buyerOrderRecord = buyerOrderRecordMapper.selectBuyerOrderRecordById(id);
        // 设置订单15分钟之后过期
        Date closeDate = DateUtil.offset(buyerOrderRecord.getCreateTime(), DateField.MINUTE, 15);
        Date nowDate = DateUtils.getNowDate();
        int compare = DateUtil.compare(closeDate, nowDate);
        // 计算订单关闭秒数
        long closeSecond = 0;
        if (compare > 0) {
            closeSecond = DateUtil.between(closeDate, nowDate, DateUnit.SECOND);
            buyerOrderRecord.setCloseSecond(closeSecond);
        }
        return buyerOrderRecord;
    }

    /**
     * 查询订单记录列表
     *
     * @param buyerOrderRecord 订单记录
     * @return 订单记录
     */
    @Override
    public List<BuyerOrderRecord> selectBuyerOrderRecordList(BuyerOrderRecord buyerOrderRecord) {
        if (buyerOrderRecord.getPayType() != null && buyerOrderRecord.getPayType().equals("全部")) {
            buyerOrderRecord.setPayType(null);
        }
        if (buyerOrderRecord.getMemberPayDuration() != null && buyerOrderRecord.getMemberPayDuration().equals("全部")) {
            buyerOrderRecord.setMemberPayDuration(null);
        }
        if (buyerOrderRecord.getBillStatus() != null && buyerOrderRecord.getBillStatus().equals("全部")) {
            buyerOrderRecord.setOrderStatus(null);
        }
        return buyerOrderRecordMapper.selectBuyerOrderRecordList(buyerOrderRecord);
    }

    /**
     * 统计订单趋势
     *
     * @param buyerOrderRecord 订单记录
     * @return 订单趋势
     */
    @Override
    public BigDecimal selectTrendBuyerOrderRecord(BuyerOrderRecord buyerOrderRecord) {
        return buyerOrderRecordMapper.selectTrendBuyerOrderRecord(buyerOrderRecord);
    }

    /**
     * 查询订单记录数量
     *
     * @param buyerOrderRecord 订单记录
     * @return 订单记录数量
     */
    @Override
    public int selectCountBuyerOrderRecord(BuyerOrderRecord buyerOrderRecord) {
        return buyerOrderRecordMapper.selectCountBuyerOrderRecord(buyerOrderRecord);
    }

    /**
     * 查询消费人数量
     *
     * @return 消费人数量
     */
    @Override
    public int selectDistinctCountBuyerOrderRecord() {
        return buyerOrderRecordMapper.selectDistinctCountBuyerOrderRecord();
    }

    /**
     * 新增订单记录
     *
     * @param buyerOrderRecord 订单记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerOrderRecord(BuyerOrderRecord buyerOrderRecord) {

        // 修改商品信息
        BuyerGoods buyerGoods = buyerGoodsMapper.selectBuyerGoodsById(buyerOrderRecord.getGoodsId());
        if(null == buyerGoods.getSaleAmount()){
            buyerGoods.setSaleAmount(0);
        }
        buyerGoods.setSaleAmount(buyerGoods.getSaleAmount() + 1);
        buyerGoodsMapper.updateBuyerGoods(buyerGoods);

        // 修改公司信息
        BuyerCompanyInfo buyerCompanyInfo = buyerCompanyInfoMapper.selectBuyerCompanyInfoById(SecurityUtils.getCompanyId());
        // 公司过期状态修改成正常
        buyerCompanyInfo.setIsExpire("0");
        // 更新企业会员信息
        String unit = buyerGoods.getUnit();
        Date expireTime = buyerCompanyInfo.getExpireTime();
        if (expireTime == null) {
            expireTime = DateUtils.getNowDate();
        }
        Date nowDate = DateUtils.getNowDate();
        // 过期时间和当日时间比较
        int compare = DateUtil.compare(expireTime, nowDate);
        expireTime = compare >= 0 ? expireTime : nowDate;
        // 重新设置会员企业到期时间
        if (unit != null) {
            switch (unit) {
                case "year":
                    expireTime = DateUtil.offset(expireTime, DateField.YEAR, buyerGoods.getDuration());
                    break;
                case "month":
                    expireTime = DateUtil.offset(expireTime, DateField.MONTH, buyerGoods.getDuration());
                    break;
                case "day":
                    expireTime = DateUtil.offset(expireTime, DateField.DAY_OF_MONTH, buyerGoods.getDuration());
                    break;
                case "hour":
                    expireTime = DateUtil.offset(expireTime, DateField.HOUR, buyerGoods.getDuration());
                    break;
            }
        }
        buyerCompanyInfo.setExpireTime(expireTime);
        buyerCompanyInfo.setUpdateTime(DateUtils.getNowDate());
        buyerCompanyInfoMapper.updateBuyerCompanyInfo(buyerCompanyInfo);

        // 新增订单
        buyerOrderRecord.setCreateTime(DateUtils.getNowDate());
        buyerOrderRecord.setGoodsName(buyerGoods.getName());
        buyerOrderRecord.setMemberPayDuration(buyerGoods.getDuration());
        buyerOrderRecord.setUnit(buyerGoods.getUnit());
        buyerOrderRecord.setDeptId(SecurityUtils.getCompanyId());
        buyerOrderRecord.setOrderStatus("0");
        buyerOrderRecord.setPayPrice(buyerGoods.getPrice());
        buyerOrderRecord.setPrice(buyerGoods.getPrice());
        buyerOrderRecord.setName(SecurityUtils.getLoginUser().getUser().getNickName());
        buyerOrderRecord.setPhone(SecurityUtils.getUsername());
        return buyerOrderRecordMapper.insertBuyerOrderRecord(buyerOrderRecord);
    }

    /**
     * 修改订单记录
     *
     * @param buyerOrderRecord 订单记录
     * @return 结果
     */
    @Override
    public int updateBuyerOrderRecord(BuyerOrderRecord buyerOrderRecord) {
        buyerOrderRecord.setUpdateTime(DateUtils.getNowDate());
        return buyerOrderRecordMapper.updateBuyerOrderRecord(buyerOrderRecord);
    }

    /**
     * 批量删除订单记录
     *
     * @param ids 需要删除的订单记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOrderRecordByIds(String[] ids) {
        return buyerOrderRecordMapper.deleteBuyerOrderRecordByIds(ids);
    }

    /**
     * 删除订单记录信息
     *
     * @param id 订单记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerOrderRecordById(String id) {
        return buyerOrderRecordMapper.deleteBuyerOrderRecordById(id);
    }
}
