package com.ruoyi.orderManagement.BasicData.service.impl;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.orderManagement.BasicData.domain.FleetCompanyMsg;
import com.ruoyi.orderManagement.BasicData.service.IFleetCompanyMsgService;
import com.ruoyi.orderManagement.BasicData.mapper.FleetCompanyMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 【车队】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@Service
public class FleetCompanyMsgServiceImpl implements IFleetCompanyMsgService
{
    @Autowired
    private FleetCompanyMsgMapper fleetCompanyMsgMapper;

    /**
     * 查询【车队】
     * 
     * @param id 【车队】ID
     * @return 【车队】
     */
    @Override
    public FleetCompanyMsg selectFleetCompanyMsgById(Long id)
    {
        return fleetCompanyMsgMapper.selectFleetCompanyMsgById(id);
    }

    /**
     * 查询【车队】列表
     * 
     * @param fleetCompanyMsg 【车队】
     * @return 【车队】
     */
    @Override
    public List<FleetCompanyMsg> selectFleetCompanyMsgList(FleetCompanyMsg fleetCompanyMsg)
    {
        return fleetCompanyMsgMapper.selectFleetCompanyMsgList(fleetCompanyMsg);
    }

    @Override
    public List<Map<String, Object>> selectFleetCompanyMsgListMap(FleetCompanyMsg fleetCompanyMsg) {
        return fleetCompanyMsgMapper.selectFleetCompanyMsgListMap(fleetCompanyMsg);
    }

    /**
     * 新增【车队】
     * 
     * @param fleetCompanyMsg 【车队】
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertFleetCompanyMsg(FleetCompanyMsg fleetCompanyMsg)
    {
        // 检验必填
        if(StringUtils.isNull(fleetCompanyMsg.getCompanyName()) ||
                StringUtils.isNull(fleetCompanyMsg.getCompanyAddress()) ||
                StringUtils.isNull(fleetCompanyMsg.getCompanyName()) ||
                StringUtils.isNull(fleetCompanyMsg.getOpeningBank()) ||
                StringUtils.isNull(fleetCompanyMsg.getBankAccount()) ||
                StringUtils.isNull(fleetCompanyMsg.getLoginAccount()) ||
                StringUtils.isNull(fleetCompanyMsg.getLinkPhone()) ){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("请填写必输项");
        }
        if(null!=fleetCompanyMsg.getId() && !fleetCompanyMsg.getId().equals("")){
            fleetCompanyMsg.setUpdateBy(SecurityUtils.getUsername());
            fleetCompanyMsg.setUpdateTime(new Date());
            fleetCompanyMsgMapper.updateFleetCompanyMsg(fleetCompanyMsg);
            return AjaxResult.success();
        }else {
            fleetCompanyMsg.setCreateBy(SecurityUtils.getUsername());
            fleetCompanyMsg.setCreatTime(new Date());
            fleetCompanyMsgMapper.insertFleetCompanyMsg(fleetCompanyMsg);
            return AjaxResult.success();
        }
    }

    /**
     * 修改【车队】
     * 
     * @param fleetCompanyMsg 【车队】
     * @return 结果
     */
    @Override
    public int updateFleetCompanyMsg(FleetCompanyMsg fleetCompanyMsg)
    {
        fleetCompanyMsg.setUpdateTime(new Date());
        return fleetCompanyMsgMapper.updateFleetCompanyMsg(fleetCompanyMsg);
    }

    /**
     * 批量删除【车队】
     * 
     * @param ids 需要删除的【车队】ID
     * @return 结果
     */
    @Override
    public int deleteFleetCompanyMsgByIds(Long[] ids)
    {
        return fleetCompanyMsgMapper.deleteFleetCompanyMsgByIds(ids);
    }

    /**
     * 删除【车队】信息
     * 
     * @param id 【车队】ID
     * @return 结果
     */
    @Override
    public int deleteFleetCompanyMsgById(Long id)
    {
        return fleetCompanyMsgMapper.deleteFleetCompanyMsgById(id);
    }

    @Override
    public String checkUFNoUnique(FleetCompanyMsg fleetCompanyMsg) {
        FleetCompanyMsg fleetCompanyMsg1 = fleetCompanyMsgMapper.checkFNoUnique(fleetCompanyMsg.getCompanyName());
        if (StringUtils.isNotNull(fleetCompanyMsg1) && fleetCompanyMsg1.getId()!=fleetCompanyMsg.getId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
