package com.gree.mall.manager.logic.workorder;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.manager.bean.workorder.PgOrderProductDetailVO;
import com.gree.mall.manager.bean.workorder.WorkerOrderDetail;
import com.gree.mall.manager.enums.PayStatusEnum;
import com.gree.mall.manager.enums.workorder.OrderBaseStatusEnum;
import com.gree.mall.manager.enums.workorder.OrderOperatorLogTypeEnum;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChangeOrderLogic {

    @Autowired
    PgOrderBaseService pgOrderBaseService;

    @Autowired
    UserService userService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    PgOrderWorkerService pgOrderWorkerService;


    @Autowired
    PgOrderProductService pgOrderProductService;

    @Autowired
    PgOrderDetailService pgOrderDetailService;

    @Autowired
    WorkerOrderService workerOrderService;

    @Autowired
    WorkerOrderItemService workerOrderItemService;


    @Autowired
    OrderLogLogic orderLogLogic;

    @Autowired
    PgOrderProductDetailService pgOrderProductDetailService;


    @Transactional(rollbackFor = Exception.class)
    public void changeAppointmentTime(String id, String appointmentTime, String appointmentEndTime,String remark) {
        pgOrderBaseService.lambdaUpdate()
                .eq(PgOrderBase::getId,id)
                .set(!StringUtils.isEmpty(remark),PgOrderBase::getChangeRemark,remark)
                .set(PgOrderBase::getAppointmentTime,appointmentTime)
                .set(PgOrderBase::getAppointmentEndTime,appointmentEndTime)
                .set(PgOrderBase::getLastOperatorAppointmentTime,new Date())
                .update();

//        pgOrderBaseService.lambdaUpdate()
//                .eq(PgOrderBase::getId,id)
//                .eq(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DYY.getKey())
//                .set(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DSHPG).update();

        this.addLog(id);
    }

    private void addLog(String id) {
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(id);

        PgOrderOperatorLog pgOrderOperatorLog = new PgOrderOperatorLog();
        pgOrderOperatorLog.setOrderBaseId(id);
        pgOrderOperatorLog.setType(OrderOperatorLogTypeEnum.GAIYUE.getRemark());
        pgOrderOperatorLog.setTypeText(OrderOperatorLogTypeEnum.GAIYUE.getRemark());
        pgOrderOperatorLog.setAppointmentTime(pgOrderBase.getAppointmentTime());
        pgOrderOperatorLog.setContent(pgOrderBase.getChangeRemark());
        orderLogLogic.addLog(pgOrderOperatorLog);
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeWorker(List<String> ids, List<String> workerIds, String websitId) {

        ids = ids.stream().distinct().collect(Collectors.toList());
        workerIds = workerIds.stream().distinct().collect(Collectors.toList());
        //循环师傅获得网点派工数据
        AdminWebsit adminWebsit = adminWebsitService.getById(websitId);

        List<PgOrderWorker> pgOrderWorkers = new ArrayList<>();



        boolean isDa = true;

        User userOne = userService.getById(workerIds.get(0));
        for (String workerId : workerIds) {
            User user = userService.getById(workerId);
            for (String id : ids) {

                PgOrderWorker pgOrderWorker = new PgOrderWorker();
                pgOrderWorker.setOrderBaseId(id);
                pgOrderWorker.setWebsitId(adminWebsit.getWebsitId());
                pgOrderWorker.setWebsitName(adminWebsit.getName());
                pgOrderWorker.setWorkerName(user.getNickName());
                pgOrderWorker.setWorkerId(workerId);
                pgOrderWorker.setWorkerIdcard(user.getIdCard());
                pgOrderWorker.setWorkerMobile(user.getMobile());
                if (userOne.getIdCard().equals(user.getIdCard()))
                    pgOrderWorker.setIsMaster(isDa);
                else
                    pgOrderWorker.setIsMaster(false);
                pgOrderWorkers.add(pgOrderWorker);
            }

        }


        pgOrderBaseService.lambdaUpdate()
                .in(PgOrderBase::getId,ids)
                .set(PgOrderBase::getWorkerName,userOne.getNickName())
                .set(PgOrderBase::getWebsitId,websitId)
                .set(PgOrderBase::getWebsitName,adminWebsit.getName())
                .set(PgOrderBase::getWebsitPhone,adminWebsit.getWebsitPhone())
                .set(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DJD.getKey())
                .set(PgOrderBase::getOrderStatusText,OrderBaseStatusEnum.DJD.getRemark())
                .set(PgOrderBase::getWorkerIdcard,userOne.getIdCard())
                .set(PgOrderBase::getWorkerId,userOne.getUserId())
                .set(PgOrderBase::getWorkerMobile,userOne.getMobile())
                .set(PgOrderBase::getDispatchTime,new Date())
                .set(PgOrderBase::getIsQd,false)
                .update();


        pgOrderWorkerService.lambdaUpdate().in(PgOrderWorker::getOrderBaseId,ids).remove();

        pgOrderWorkerService.saveBatch(pgOrderWorkers);

        for (String id : ids) {
            this.addWorkerLog(id);
        }
    }

    private void addWorkerLog(String id) {
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(id);

        PgOrderOperatorLog pgOrderOperatorLog = new PgOrderOperatorLog();
        pgOrderOperatorLog.setOrderBaseId(id);
        pgOrderOperatorLog.setType(OrderOperatorLogTypeEnum.GP.getKey());
        pgOrderOperatorLog.setTypeText(OrderOperatorLogTypeEnum.GP.getRemark());
        pgOrderOperatorLog.setContent("派工主要工程师："+pgOrderBase.getWorkerName());
        orderLogLogic.addLog(pgOrderOperatorLog);
    }


    public List<PgOrderProductDetail> getOrderProduct(String id, List<String> status) {

        return pgOrderProductDetailService.lambdaQuery().eq(PgOrderProductDetail::getOrderBaseId,id)
                .in(!CollectionUtils.isEmpty(status),PgOrderProductDetail::getStatus,status).list();
    }

    public PgOrderProductDetailVO productDetail(String id) {
        PgOrderProductDetail pgOrderProduct = pgOrderProductDetailService.getById(id);

        PgOrderProductDetailVO pgOrderProductDetail = BeanUtil.toBean(pgOrderProduct, PgOrderProductDetailVO.class);

        List<PgOrderDetail> list = pgOrderDetailService.lambdaQuery().eq(PgOrderDetail::getProductDetailId, id).list();

        pgOrderProductDetail.setPgOrderProductDetails(list);
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(pgOrderProduct.getOrderBaseId());

        pgOrderProductDetail.setPgOrderBase(pgOrderBase);

        List<PgOrderWorker> list1 = pgOrderWorkerService.lambdaQuery().eq(PgOrderWorker::getOrderBaseId, pgOrderProductDetail.getOrderBaseId()).list();
        pgOrderProductDetail.setPgOrderWorkers(list1);

        return pgOrderProductDetail;

    }


    public List<WorkerOrderDetail> getOrderList(String workerOrderId) {

        List<WorkerOrder> list = workerOrderService.lambdaQuery().eq(WorkerOrder::getWorkerOrderId, workerOrderId)
                .eq(WorkerOrder::getPayStatus, PayStatusEnum.PAID.getKey()).list();
        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();

        List<WorkerOrderDetail> workerOrderDetails1 = BeanUtil.copyToList(list, WorkerOrderDetail.class);
        for (WorkerOrderDetail workerOrder : workerOrderDetails1) {

            workerOrder.setWorkerOrderItems(workerOrderItemService.lambdaQuery()
                    .eq(WorkerOrderItem::getOrderId,workerOrder.getOrderId()).list());

            PgOrderBase pgOrderBase = pgOrderBaseService.getById(workerOrderId);
            workerOrder.setPgOrderBase(pgOrderBase);


        }




        return workerOrderDetails1;

    }
}
