package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ZccmUserPriceTotal;
import com.hc.business.mapper.ZccmOrdersMapper;
import com.hc.business.model.ZccmOrders;
import com.hc.business.service.ZccmOrdersService;
import com.hc.core.utils.enums.InfoEnum;
import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.status.CodeFlagEnum;
import com.hc.core.utils.status.OrdersFlagEnum;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 订单表(ZccmOrders)表服务实现类
 *
 * @author java
 * @since 2020-11-26 16:46:46
 */

@Service("ordersService")
public class ZccmOrdersServiceImpl implements ZccmOrdersService {

    private final ZccmOrdersMapper ordersMapper;

    public ZccmOrdersServiceImpl(ZccmOrdersMapper ordersMapper) {
        this.ordersMapper = ordersMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param ordersId 主键
     * @return 实例对象
     */
    @Override
    public ZccmOrders queryById(Integer ordersId) {
        return this.ordersMapper.selectByPrimaryKey(ordersId);
    }

    /**
     * 新增数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmOrders insert(ZccmOrders orders) {
        String orderNo = orders.getOrderNo().trim();
        ZccmOrders orders1 = this.ordersMapper.queryByOrderNo(orderNo);
        if (!Objects.isNull(orders1)) {
            throw new ResponseException(InfoEnum.ORDER_ERROR);
        }
        orders.setCreateTime(System.currentTimeMillis());
        orders.setOrderStatus(OrdersFlagEnum.ORDERS_FLAG_UNPAID.getCode());
        orders.setIsDe(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
        this.ordersMapper.insert(orders);
        return orders;
    }

    /**
     * 创建订单编号
     *
     * @return 实例对象
     */
    @Override
    public String createOrderNo() {
        String mills = String.valueOf(System.currentTimeMillis());
        String random = RandomStringUtils.randomNumeric(4);
        String s = mills + random;
        return s;
    }

    /**
     * 修改数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmOrders update(ZccmOrders orders) {
        this.ordersMapper.updateByPrimaryKeySelective(orders);
        return this.queryById(orders.getOrdersId());
    }

    /**
     * 通过主键删除数据
     *
     * @param ordersId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer ordersId) {
//        return this.ordersMapper.deleteByPrimaryKey(ordersId) > 0;
        ZccmOrders orders = this.ordersMapper.selectByPrimaryKey(ordersId);
        if (Objects.isNull(orders)) {
            return false;
        }
        orders.setIsDe(CodeFlagEnum.OBJECT_DISABLED_CODE.getCode());
        return this.ordersMapper.updateByPrimaryKey(orders) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.ordersMapper::selectAll);
    }


    @Override
    public PageInfo queryAllWithOrderInvoices(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.ordersMapper::queryAllWithInvoices);
    }

    @Override
    public PageInfo queryAllByUserId(int page, int size, Integer userId) {
        PageHelper.startPage(page, size);
        List<ZccmOrders> orders = this.ordersMapper.queryAllByUserId(userId);
        return new PageInfo(orders);
    }

    @Override
    public PageInfo queryAllByClassId(int page, int size, Integer classId) {
        PageHelper.startPage(page, size);
        List<ZccmOrders> orders = this.ordersMapper.queryAllByClassId(classId);
        return new PageInfo(orders);
    }

    @Override
    public PageInfo queryAllByClassName(int page, int size, String className) {
        PageHelper.startPage(page, size);
        List<ZccmOrders> orders = this.ordersMapper.queryAllByClassName(className);
        return new PageInfo(orders);
    }

    @Override
    public PageInfo queryAllByStatus(int page, int size, Integer status) {
        PageHelper.startPage(page, size);
        List<ZccmOrders> orders = this.ordersMapper.queryAllByStatus(status);
        return new PageInfo<>(orders);
    }

    @Override
    public List<ZccmOrders> queryByUserInvoicesId(Integer userInvoicesId) {
        return this.ordersMapper.queryByUserInvoicesId(userInvoicesId);
    }

    @Override
    public ZccmUserPriceTotal queryUserCount() {
        ZccmUserPriceTotal zccmUserPriceTotal = new ZccmUserPriceTotal();
        zccmUserPriceTotal.setTotalUserCount(this.ordersMapper.countPrice());
        zccmUserPriceTotal.setTodayUserCount(this.ordersMapper.queryToday());
        zccmUserPriceTotal.setMonthUserCount(this.ordersMapper.queryMonth());
        zccmUserPriceTotal.setYearUserCount(this.ordersMapper.queryYear());
        return zccmUserPriceTotal;
    }

    @Override
    public List<ZccmOrders> queryByOrderNo(String orderNo) {
        List<ZccmOrders> ordersList = new ArrayList<>();
        ZccmOrders orders = this.ordersMapper.queryByOrderNo(orderNo);
        if (Objects.isNull(orders)) {
            return Collections.emptyList();
        }
        ordersList.add(orders);
        return ordersList;
    }

    @Override
    public Integer countOrderTotal() {
        return this.ordersMapper.countOrderTotal();
    }

}