package cn.ezhizao.project.business.upgrade.service.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.ezhizao.project.business.upgrade.mapper.BizWorkOrderUpdateMapper;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrder;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrderDetail;
import cn.ezhizao.project.business.workOrder.service.impl.BizWorkOrderDetailServiceImpl;
import cn.ezhizao.project.business.workOrder.service.impl.BizWorkOrderServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.ezhizao.project.business.upgrade.domain.BizWorkOrderUpdate;
import cn.ezhizao.project.business.upgrade.service.IBizWorkOrderUpdateService;

/**
 * 企业租户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-14
 */
@Service
public class BizWorkOrderUpdateServiceImpl  extends ServiceImpl<BizWorkOrderUpdateMapper, BizWorkOrderUpdate> implements IBizWorkOrderUpdateService
{
    @Resource
    private BizWorkOrderUpdateMapper bizWorkOrderUpdateMapper;
    @Resource
    private BizWorkOrderServiceImpl bizWorkOrderService;
    @Resource
    private BizWorkOrderDetailServiceImpl bizWorkOrderDetailService;
    @Autowired
    private BizWorkOrderDetailUpdateServiceImpl bizWorkOrderDetailUpdateService;

    /**
     * 查询企业租户列表
     *
     * @param bizWorkOrderUpdate 企业租户
     * @return 企业租户
     */
    @Override
    public List<BizWorkOrderUpdate> getList(BizWorkOrderUpdate bizWorkOrderUpdate)
    {
        return bizWorkOrderUpdateMapper.getList(bizWorkOrderUpdate);
    }
    /**
     * 物理删除
     * @param  ids
     * @return 删除结果
     */
    @Override
    public int physicalDelete(List<Long> ids) {
        return bizWorkOrderUpdateMapper.physicalDelete(ids);
    }


    @Override
    public List<BizWorkOrderUpdate> getList2(BizWorkOrderUpdate bizWorkOrder) {
        return bizWorkOrderUpdateMapper.getList2(bizWorkOrder);
    }

    @Override
    public List<BizWorkOrderUpdate> gettoUpLoadList(BizWorkOrderUpdate bizWorkOrder) {
        return bizWorkOrderUpdateMapper.gettoUpLoadList(bizWorkOrder);
    }

    @Override
    public void workerUpgrade(List<BizWorkOrder> waitingForUpgradingList, Long tenantId) {
        //需要把工单和工单详情中的合同和合同详情id设置为0
        bizWorkOrderService.updateByTenantId(tenantId);
        bizWorkOrderDetailService.updateByTenantId(tenantId);
        //这个是查询
        BizWorkOrder query = new BizWorkOrder();
        query.setTenantId(tenantId);
        BizWorkOrderDetail query2 = new BizWorkOrderDetail();
        query2.setTenantId(tenantId);
        //从BizWorkOrderUpdate中获取获取同账套下的工单 并且不用BizWorkOrderUpdate接受，改用BizWorkOrder 保存时不用赋值了
        List<BizWorkOrder> bizWorkOrderList = bizWorkOrderUpdateMapper.selectByUpgradeId(query);
        bizWorkOrderList.forEach(bizWorkOrder -> bizWorkOrder.setVersion(null));
        List<BizWorkOrderDetail> bizWorkOrderDetailList = bizWorkOrderDetailUpdateService.selectByUpgradeId(query2);
        bizWorkOrderDetailList.forEach(bizWorkOrderDetail -> bizWorkOrderDetail.setVersion(null));
        bizWorkOrderService.saveOrUpdateBatch(bizWorkOrderList);
        bizWorkOrderDetailService.saveOrUpdateBatch(bizWorkOrderDetailList);

        int i = bizWorkOrderDetailService.deleteDuplicateId(tenantId);

        System.out.println(i);
    }

    @Override
    public Long getToUpLoadCount(BizWorkOrderUpdate bizWorkOrder) {
        return bizWorkOrderUpdateMapper.getToUpLoadCount(bizWorkOrder);
    }

    @Override
    public List<BizWorkOrder> getNewOrder() {
        return bizWorkOrderUpdateMapper.getNewOrder();
    }
    @Override
    public List<BizWorkOrder> getStopTaskOrder() {
        return bizWorkOrderUpdateMapper.getStopTaskOrder();
    }
}
