package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.order.StoreOrderReturn;
import com.zbkj.common.model.order.StoreOrderReturnInfo;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.ApplyRecordsResponse;
import com.zbkj.service.dao.StoreOrderReturnDao;
import com.zbkj.service.service.StoreOrderReturnInfoService;
import com.zbkj.service.service.StoreOrderReturnService;
import com.zbkj.service.service.StoreOrderService;
import com.zbkj.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

@Service
public class StoreOrderReturnServiceImpl extends ServiceImpl<StoreOrderReturnDao, StoreOrderReturn> implements StoreOrderReturnService {

    @Autowired private StoreOrderReturnDao dao;
    @Autowired private UserService userService;
    @Autowired @Lazy private StoreOrderService storeOrderService;
    @Autowired private StoreOrderReturnInfoService storeOrderReturnInfoService;

    @Override
    public void createReturn(StoreOrderReturn oReturn) {
        dao.insert(oReturn);
        oReturn.getReturnInfos().forEach(info -> info.setReturnId(oReturn.getId()));
        storeOrderReturnInfoService.saveBatch(oReturn.getReturnInfos());
    }

    @Override
    public void refundErr(Integer orderId, String verifyFailReason) {
        LambdaQueryWrapper<StoreOrderReturn> lw = new LambdaQueryWrapper<>();
        lw.eq(StoreOrderReturn::getOrderId, orderId);
        StoreOrderReturn one = getOne(lw);
        StoreOrderReturn upd = new StoreOrderReturn();
        upd.setId(one.getId());
        upd.setReturnVerifyDate(new Date());
        upd.setRefundVerifyFailReason(verifyFailReason);
        upd.setReturnState(3);
        dao.updateById(upd);
    }

    @Override
    public void refundOk(Integer orderId) {
        LambdaQueryWrapper<StoreOrderReturn> lw = new LambdaQueryWrapper<>();
        lw.eq(StoreOrderReturn::getOrderId, orderId);
        StoreOrderReturn one = getOne(lw);
        StoreOrderReturn upd = new StoreOrderReturn();
        upd.setId(one.getId());
        upd.setReturnVerifyDate(new Date());
        upd.setReturnState(2);
        dao.updateById(upd);
    }

    @Override
    public CommonPage<ApplyRecordsResponse> applyRecordsList(PageParamRequest page) {
        Integer userId = userService.getUserIdException();
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<ApplyRecordsResponse> list = dao.applyRecordsList(userId, null);
        PageHelper.clearPage();
        for (ApplyRecordsResponse ar : list) {
            ar.setReturnInfoList(storeOrderReturnInfoService.selectByReturnId(ar.getId()));
        }
        return CommonPage.restPage(list);
    }

    @Override
    public ApplyRecordsResponse getApplyRecord(String id) {
        Integer userId = userService.getUserIdException();
        List<ApplyRecordsResponse> list = dao.applyRecordsList(userId, Integer.parseInt(id));
        if (list.isEmpty()) { throw new CrmebException("未找到信息"); }

        ApplyRecordsResponse response = list.get(0);
        List<StoreOrderReturnInfo> returnInfoList = storeOrderReturnInfoService.selectByReturnId(response.getId());
        response.setReturnInfoList(returnInfoList);
        response.setReturnNum(returnInfoList.stream().map(StoreOrderReturnInfo::getReturnNum).reduce(0, Integer::sum));
        return response;
    }
}
