package com.tykj.dev.device.repair.service.impl;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.tykj.dev.device.repair.repository.RepairBillDao;
import com.tykj.dev.device.repair.service.RepairBillService;
import com.tykj.dev.device.repair.subject.domin.RepairBill;
import com.tykj.dev.device.repair.subject.vo.RepairBillSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;
import java.lang.reflect.Field;
import java.util.Optional;

/**
 * @author dengdiyi
 */
@Service
public class RepairBillServiceImpl implements RepairBillService {

    @Autowired
    private RepairBillDao deviceRepairBillDao;

    @Override
    public RepairBill addEntity(RepairBill deviceRepairBillEntity) {
        return deviceRepairBillDao.save(deviceRepairBillEntity);
    }

    @Override
    public Page<RepairBill> getPage(RepairBillSelectVo deviceRepairBillSelectVo, Pageable pageable) {
        return deviceRepairBillDao.findAll(getSelectSpecification(deviceRepairBillSelectVo),pageable);
    }

    @Override
    public RepairBill getOne(Integer id) {
        Optional<RepairBill> resultEntity = deviceRepairBillDao.findById(id);
        return resultEntity.orElse(null);
    }

    @Override
    public RepairBill update(RepairBill deviceRepairBillEntity) {
        return deviceRepairBillDao.save(deviceRepairBillEntity);
    }

    @Override
    public void delete(Integer id) {
        deviceRepairBillDao.deleteById(id);
    }

    private Specification<RepairBill> getSelectSpecification(RepairBillSelectVo deviceRepairBillSelectVo){
        PredicateBuilder<RepairBill> predicateBuilder = Specifications.and();
        if (deviceRepairBillSelectVo!=null) {
            if (deviceRepairBillSelectVo.getReceiveUnit()!=null){
                predicateBuilder.eq("receiveUnit",deviceRepairBillSelectVo.getReceiveUnit());
            }
            if (deviceRepairBillSelectVo.getSendUnit()!=null){
                predicateBuilder.eq("sendUnit",deviceRepairBillSelectVo.getSendUnit());
            }
            if (deviceRepairBillSelectVo.getContent() != null) {
                Class<RepairBill> deviceRepairBillEntityClass = RepairBill.class;
                Field[] declaredFields = deviceRepairBillEntityClass.getDeclaredFields();
                PredicateBuilder<RepairBill> p = Specifications.or();
                for (Field field : declaredFields) {
                    if (field.getType().equals(String.class)&&field.getAnnotation(Transient.class)==null) {
                        p.like(field.getName(), "%" + deviceRepairBillSelectVo.getContent() + "%");
                    }
                }
                predicateBuilder.predicate(p.build());
            }
            if (deviceRepairBillSelectVo.getStartTime() != null) {
                predicateBuilder.gt("createTime", deviceRepairBillSelectVo.getStartTime());
            }
            if (deviceRepairBillSelectVo.getEndTime() != null) {
                predicateBuilder.lt("createTime", deviceRepairBillSelectVo.getEndTime());
            }
        }
        predicateBuilder.eq("repairStatus",5);
        return predicateBuilder.build();
    }
}
