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

import com.pine.admin.modules.business.dao.LogisticsCompanyDao;
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 java.util.Objects;

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

import com.pine.admin.modules.business.dao.LogisticsTemplateDao;
import com.pine.admin.modules.business.entity.LogisticsTemplate;
import com.pine.admin.modules.business.service.LogisticsTemplateService;


@Service("logisticsTemplateService")
@Slf4j
public class LogisticsTemplateServiceImpl implements LogisticsTemplateService {

    @Autowired
    private LogisticsTemplateDao logisticsTemplateDao;
    @Autowired
    private LogisticsCompanyDao logisticsCompanyDao;

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

        log.info("getLogisticsTemplateList");
        try {

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

        return null;
    }

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

        log.info("getLogisticsTemplateByPage");
        try {

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

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

        return null;
    }

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

        log.info("getLogisticsTemplateById");
        try {

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

        return null;
    }

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

        log.info("getLogisticsTemplate");
        try {

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

        return null;
    }

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

        log.info("createLogisticsTemplate");
        try {

            int updates = logisticsTemplateDao.insertSelective(record);

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

        return false;
    }

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

        log.info("deleteLogisticsTemplate");
        try {

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

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

        return false;
    }

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

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

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

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

        return false;
    }

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

        log.info("updateLogisticsTemplate");
        try {

            int updates = logisticsTemplateDao.updateByPrimaryKeySelective(record);

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

        return false;
    }

    @Override
    public LogisticsTemplate queryLogisticsTemplateWithCompany(long id) {

        log.debug("queryLogisticsTemplateWithCompany and id:{} \r\n storeId;{}", id);

        LogisticsTemplate logisticsTemplate = logisticsTemplateDao.byId(id);

        if (Objects.isNull(logisticsTemplate)) {
            return logisticsTemplate;
        }
        System.out.println(">>>>>>>   " +logisticsTemplate.getCompanyId());
        logisticsTemplate.setLogisticsCompany(logisticsCompanyDao.byId(logisticsTemplate.getCompanyId()));
        return logisticsTemplate;
    }
}
