package com.pj.api.service;

import cn.hutool.core.bean.BeanUtil;
import com.pj.api.bo.InOutRecordBO;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import com.pj.utils.so.SoMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 */

@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ApiService {

    @Resource
    TbCostomerService tbCostomerService;
    @Resource
    TbBusinessService tbBusinessService;

    public List<InOutRecordBO> getInOutRecord(SoMap so){
        List<TbBusiness> businessList = tbBusinessService.getList(so);
        List<InOutRecordBO> recordList = new ArrayList<>();
        for (TbBusiness business : businessList) {
            InOutRecordBO record = new InOutRecordBO();
            BeanUtil.copyProperties(business, record);
            TbCostomer costomer = tbCostomerService.getById(business.getCustomerId());
            record.setBusinessId(business.getId()).setCustomerContact(costomer.getPhone()).setDutyPeople(costomer.getDutyPeople());
            if(record.getRealInTime() != null && record.getOutDayTime() != null){
                record.setStatus(3);//已出场
            }else if(record.getRealInTime() != null && record.getOutDayTime() == null){
                record.setStatus(2);//已入场
            }else{
                record.setStatus(1);//未入场
            }
            recordList.add(record);
        }
        return recordList;
    }

    public List<TbCostomer> getCustomerList(SoMap so) {
        List<TbCostomer> list = tbCostomerService.getList(so);
        return list;
    }

    public void confirmCustomer(Long customerId) {
        //
        TbCostomer costomer = tbCostomerService.getById(customerId);
        if(costomer != null && costomer.getJudgeStatus() == 1){
            tbCostomerService.judge(customerId + "", 2, "H5端审核");
        }
    }
}
