package com.pine.admin.modules.business.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.pine.admin.modules.business.dao.CustomerDao;
import com.pine.admin.modules.business.entity.Customer;
import com.pine.admin.modules.business.service.CustomerService;


@Service("customerService")
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;


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

        log.info("getCustomerList");
        try {

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

        return null;
    }

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

        log.info("getCustomerByPage");
        try {

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

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

        return null;
    }

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

        log.info("getCustomerById");
        try {

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

        return null;
    }

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

        log.info("getCustomer");
        try {

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

        return null;
    }

    @Override
    @Transactional
    public boolean create(Customer record) {

        log.info("createCustomer");
        try {

            int updates = customerDao.insertSelective(record);

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

        return false;
    }

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

        log.info("deleteCustomer");
        try {

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

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

        return false;
    }

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

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

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

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

        return false;
    }

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

        log.info("updateCustomer");
        try {

            int updates = customerDao.updateByPrimaryKeySelective(record);

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

        return false;
    }

}
