package com.pine.admin.modules.business.service.impl;
import java.util.*;

import com.pine.common.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import com.pine.common.dto.Page;
import org.springframework.transaction.annotation.Transactional;

import com.pine.admin.modules.business.dao.CarBackOrderDao;
import com.pine.admin.modules.business.entity.CarBackOrder;
import com.pine.admin.modules.business.service.CarBackOrderService;


@Service("carBackOrderService")
@Slf4j
public class CarBackOrderServiceImpl implements CarBackOrderService {

    @Autowired
    private CarBackOrderDao carBackOrderDao;


    @Override
    @Transactional
    public List<CarBackOrder> getAllList(CarBackOrder record) {

        log.info("getCarBackOrderList");
        try {

            return carBackOrderDao.queryAll(record);
        } catch (Exception e) {
            log.error("getCarBackOrderList", e);
        }

        return null;
    }

    @Override
    @Transactional
    public List<CarBackOrder> getByPage(Page page, CarBackOrder record) {

        log.info("getCarBackOrderByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put("page", page);

            return carBackOrderDao.queryByPage(parameter);
        } catch (Exception e) {
            log.error("getCarBackOrderByPage", e);
        }

        return null;
    }

    @Override
    @Transactional
    public CarBackOrder getById(String id) {

        log.info("getCarBackOrderById");
        try {

            return carBackOrderDao.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            log.error("getCarBackOrderById", e);
        }

        return null;
    }

    @Override
    @Transactional
    public CarBackOrder getOne(CarBackOrder record) {

        log.info("getCarBackOrder");
        try {

            return carBackOrderDao.queryOne(record);
        } catch (Exception e) {
            log.error("getCarBackOrder", e);
        }

        return null;
    }
    @Override
    @Transactional
    public boolean create(CarBackOrder record) {

        log.info("createCarBackOrder");
        try {
            Random random = new Random();
            long l = new SnowflakeIdWorker(31L, 31L).nextId();
            record.setBackCode(String.valueOf(l));
            record.setStatus("1");
            record.setCreateTime(new Date());
            int updates = carBackOrderDao.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("createCarBackOrder", e);
        }

        return false;
    }

    @Override
    @Transactional
    public boolean delete(String id) {

        log.info("deleteCarBackOrder");
        try {

            int updates = carBackOrderDao.delete(Integer.valueOf(id));

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("deleteCarBackOrder", e);
        }

        return false;
    }

    @Override
    @Transactional
    public boolean deltetByIds(String ids) {

        log.info("deleteeCarBackOrder byIDS");
        try {

            String[] id = ids.split(";");
            int updates = carBackOrderDao.deleteIds(id);

            if (id.length == updates) {
                return true;
            }
        } catch (Exception e) {
            log.error("deleteeCarBackOrder byIDS", e);
        }

        return false;
    }

    @Override
    @Transactional
    public boolean update(CarBackOrder record) {

        log.info("updateCarBackOrder");
        try {

            int updates = carBackOrderDao.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("updateCarBackOrder", e);
        }

        return false;
    }

}
