package com.bonus.sharedstation.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.sharedstation.app.domain.GreenUserInfo;
import com.bonus.sharedstation.app.dto.WashClothingOrderDTO;
import com.bonus.sharedstation.app.entity.WashClothingOrder;
import com.bonus.sharedstation.app.entity.WashClothingOrderInfo;
import com.bonus.sharedstation.app.entity.WashClothingPrice;
import com.bonus.sharedstation.app.mapper.WashClothingOrderInfoMapper;
import com.bonus.sharedstation.app.mapper.WashClothingOrderMapper;
import com.bonus.sharedstation.app.mapper.WashClothingPriceMapper;
import com.bonus.sharedstation.app.service.WashClothingOrderService;
import com.bonus.sharedstation.common.utils.OrderNumberGeneratorUtils;
import com.bonus.sharedstation.web.dto.WashClothingPriceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 便民服务信息表保洁(CleaningServicesOrder)表服务实现类
 *
 * @author makejava
 * @since 2024-12-10 17:02:11
 */
@Service("washClothingOrderService")
@Slf4j
public class WashClothingOrderServiceImpl extends ServiceImpl<WashClothingOrderMapper, WashClothingOrder> implements WashClothingOrderService {

    @Resource
    private WashClothingOrderMapper dao;
    @Resource
    private WashClothingOrderInfoMapper washClothingOrderInfoMapper;
    @Resource
    private WashClothingPriceMapper washClothingPriceMapper;

    @Resource
    private BaseService baseService;

    /**
     * 分页
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPageApp(WashClothingOrderDTO dto) {
        log.info("查询分页入参: {}", dto);
        LambdaQueryWrapper<WashClothingOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<WashClothingOrder> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        // 根据userId是否提供来动态添加条件
        if (dto.getUserId() != null) {
            queryWrapper.eq(WashClothingOrder::getUserId, dto.getUserId());
        }

        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like(WashClothingOrder::getName, dto.getName());
        }
        queryWrapper
                .eq(WashClothingOrder::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(WashClothingOrder::getCreatedTime);
        Page<WashClothingOrder> result = dao.selectPage(page, queryWrapper);
        //处理参数问题
        List<WashClothingOrder> records = result.getRecords();
        if (records == null) {
            return AjaxResult.success(result);
        }
        //查询所有的绿植订单信息
        LambdaQueryWrapper<WashClothingOrderInfo> greenOrderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        greenOrderInfoLambdaQueryWrapper
                .eq(WashClothingOrderInfo::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(WashClothingOrderInfo::getCreatedTime);
        List<WashClothingOrderInfo> greenOrderInfos = washClothingOrderInfoMapper.selectList(greenOrderInfoLambdaQueryWrapper);
        for (WashClothingOrder record : records) {
            if (record != null) {
                Integer id = record.getId();
                List<WashClothingOrderInfo> collect = greenOrderInfos.stream().
                        filter(greenOrderInfo -> greenOrderInfo.getOrderId() != null
                                && greenOrderInfo.getOrderId().equals(id)).collect(Collectors.toList());
                if (collect != null) {
                    record.setOrderInfoList(collect);
                }
            }
        }
        return AjaxResult.success(result);
    }


    /**
     * 分页
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListWeb(WashClothingOrderDTO dto) {
        log.info("查询分页入参: {}", dto);
        LambdaQueryWrapper<WashClothingOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<WashClothingOrder> page = new Page<>(dto.getCurrentPage(), dto.getLimit());

        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like(WashClothingOrder::getName, dto.getName());
        }
        queryWrapper
                .eq(WashClothingOrder::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(WashClothingOrder::getCreatedTime);

        Page<WashClothingOrder> result = dao.selectPage(page, queryWrapper);
        //处理参数问题
        List<WashClothingOrder> records = result.getRecords();
        if (records == null) {
            return AjaxResult.success(result);
        }
        //查询所有的绿植订单信息
        LambdaQueryWrapper<WashClothingOrderInfo> greenOrderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        greenOrderInfoLambdaQueryWrapper
                .eq(WashClothingOrderInfo::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(WashClothingOrderInfo::getCreatedTime);
        List<WashClothingOrderInfo> greenOrderInfos = washClothingOrderInfoMapper.selectList(greenOrderInfoLambdaQueryWrapper);
        for (WashClothingOrder record : records) {
            if (record != null) {
                Integer id = record.getId();
                List<WashClothingOrderInfo> collect = greenOrderInfos.stream().
                        filter(greenOrderInfo -> greenOrderInfo.getOrderId() != null
                                && greenOrderInfo.getOrderId().equals(id)).collect(Collectors.toList());
                if (collect != null) {
                    record.setOrderInfoList(collect);
                }
            }
        }
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult selectInfoById(WashClothingOrder dto) {
        WashClothingOrder greenOrder = dao.selectById(dto.getId());
        LambdaQueryWrapper<WashClothingOrderInfo> greenOrderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (greenOrder != null) {
            greenOrderInfoLambdaQueryWrapper
                    .eq(WashClothingOrderInfo::getIsDeleted, dto.getIsDeleted())
                    .eq(WashClothingOrderInfo::getOrderId, greenOrder.getId())
                    .orderByDesc(WashClothingOrderInfo::getCreatedTime);
            List<WashClothingOrderInfo> greenOrderInfos =
                    washClothingOrderInfoMapper.selectList(greenOrderInfoLambdaQueryWrapper);

            if (greenOrderInfos != null) {
                greenOrder.setOrderInfoList(greenOrderInfos);
            }
            return AjaxResult.success(greenOrder);
        }
        return AjaxResult.error("暂无相关数据");
    }

    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult add(WashClothingOrderDTO dto) {
        log.info("新增入参 : " + dto);
        GreenUserInfo userInfo = baseService.getUserInfo(dto.getUserId());
        //得到登录人
        WashClothingOrder greenOrder = new WashClothingOrder();
        BeanUtils.copyProperties(dto, greenOrder);
        greenOrder.setCreatedBy(userInfo.getLoginName());
        greenOrder.setCreatedTime(new Date());
        greenOrder.setUserId(userInfo.getUserId());
        greenOrder.setCustId(String.valueOf(SecurityUtils.getUserId()));
        greenOrder.setIsDeleted("0");
        greenOrder.setOrderTime(new Date());
        //流水号
        greenOrder.setOrdno(OrderNumberGeneratorUtils.generateOrderNumber());
        //
        return null;
        //新增绿植订单

    }


    @Override
    public AjaxResult updateInfoById(WashClothingOrder dto) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPage(WashClothingPriceDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<WashClothingPrice> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<WashClothingPrice> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like(WashClothingPrice::getName, dto.getName());
        }
        queryWrapper
                .eq(WashClothingPrice::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(WashClothingPrice::getCreatedTime);

        Page<WashClothingPrice> result = washClothingPriceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

}

