package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.api.domain.MaLabelBind;
import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.base.api.domain.MaType;
import com.bonus.sgzb.base.api.domain.SltAgreementApply;
import com.bonus.sgzb.base.domain.MaPropSet;
import com.bonus.sgzb.base.domain.vo.IotRecordVo;
import com.bonus.sgzb.base.mapper.*;
import com.bonus.sgzb.base.service.MaMachineService;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MaMachineServiceImpl implements MaMachineService {

    @Resource
    private MaMachineMapper maMachineMapper;

    @Resource
    private MaTypeMapper typeMapper;

    @Resource
    private MaLabelBindMapper labelBindMapper;

    @Resource
    private MaPropInfoMapper maPropInfoMapper;

    @Resource
    private BaseIotMachineMapper baseIotMachineMapper;


    @Override
    public List<MaMachine> getMaMachine(MaMachine maMachine) {
        List<MaMachine> listMaMachine = new ArrayList<>();
        listMaMachine =maMachineMapper.getMaMachine(maMachine);
        if (listMaMachine.size() > 0){
            for (MaMachine ma : listMaMachine) {
                //根据机具编码查询iot设备
                List<IotRecordVo>  iotRecordVoList = baseIotMachineMapper.selectList(ma.getMaCode());
                ma.setIotNum(iotRecordVoList.size());
                //判断是否绑定了iot设备
                if (iotRecordVoList.size()>0){
                    for (int i = 0; i < iotRecordVoList.size(); i++) {
                        //保存定位iot设备编码
                        if ("125".equals(iotRecordVoList.get(i).getIotType())){
                            ma.setIotCode(iotRecordVoList.get(i).getIotCode());
                        }
                    }
                }
            }
        }
        return listMaMachine;
    }

    /**
     *  添加查询物品种类,设备类型,规格型号
     * @param machine
     * @return
     */
    private void getType(MaMachine machine) {
        long typeId = machine.getTypeId();
        // 规格型号
        MaType maType = typeMapper.selectTypeByTypeId(typeId);
        // 设备类型
        machine.setSpecificationType(maType.getTypeName());
        MaType maType1 = typeMapper.selectTypeByTypeId(maType.getParentId());
        // 物品种类
        machine.setDeviceType(maType1.getTypeName());
        MaType maType2 = typeMapper.selectTypeByTypeId(maType1.getParentId());
        machine.setItemType(maType2.getTypeName());
    }

    /**
     * 新增机具设备
     * @param maMachine 机具设备信息
     * @return 结果
     */
    @Override
    @Transactional
    public int maMachineAdd(MaMachine maMachine) {
        int i = maMachineMapper.maMachineAdd(maMachine);
        // 新增标签绑定
        MaLabelBind maLabelBind = new MaLabelBind();
        String labelCode = maMachine.getLabelCode();
        maLabelBind.setLabelCode(labelCode);
        maLabelBind.setMaId(maMachine.getMaId());
        maLabelBind.setTypeId(maMachine.getTypeId());
        labelBindMapper.insertLabelBind(maLabelBind);

        // 新增机具和资产属性绑定关系
        MaPropSet maPropSet = new MaPropSet();
        maPropSet.setTypeId(maMachine.getTypeId());
        maPropSet.setPropId(maMachine.getPropId());
        maPropSet.setCreateTime(new Date());
        maPropSet.setCreateBy(SecurityUtils.getUsername());
        maPropSet.setCompanyId(maMachine.getCompanyId());
        maPropInfoMapper.insertMaPropSet(maPropSet);

        return i;
    }

    @Override
    public int remove(Long[] maIds) {
        int res = 0;
        if (maIds != null || maIds.length > 0) {
            for (Long maId : maIds) {
                MaMachine maMachine = maMachineMapper.selectMaMachineByMaId(maId);
                if (maMachine.getTypeId() != 0 && ("15").equals(maMachine.getMaStatus())) {
                    res = maMachineMapper.updateNum(maMachine.getTypeId());
                }
            }
        }
        res += maMachineMapper.remove(maIds);
        return res;
    }

    @Override
    @Transactional
    public int updateMaMachine(MaMachine maMachine) {
        // 删除类型和资产属性绑定关系
        maPropInfoMapper.deleteMaPropSetByPropIds(maMachine.getTypeId());
        // 新增机具和资产属性绑定关系
        MaPropSet maPropSet = new MaPropSet();
        maPropSet.setTypeId(maMachine.getTypeId());
        maPropSet.setPropId(maMachine.getPropId());
        maPropSet.setCreateTime(new Date());
        maPropSet.setCreateBy(SecurityUtils.getUsername());
        maPropSet.setCompanyId(maMachine.getCompanyId());
        maPropInfoMapper.insertMaPropSet(maPropSet);
        return maMachineMapper.updateMaMachine(maMachine);
    }

    /**
     * 查询机具设备管理ma_machine
     *
     * @param maId 机具设备管理ma_machine主键
     * @return 机具设备管理ma_machine
     */
    @Override
    public MaMachine selectMaMachineByMaId(Long maId)
    {
        return maMachineMapper.selectMaMachineByMaId(maId);
    }

    @Override
    public MaMachine getMachineByQrCode(MaMachine maMachine) {
        MaMachine ma = maMachineMapper.getMachineByQrCode(maMachine);
        if(ma != null){
            SltAgreementApply leaseProject = maMachineMapper.getLeaseProject(ma);
            SltAgreementApply backProject = maMachineMapper.getBackProject(ma);
            if (leaseProject != null){
                ma.setLeaseUnit(leaseProject.getUnitName());
                ma.setLeasePro(leaseProject.getProjectName());
                ma.setLeaseTime(leaseProject.getCreateTime());
            }
            if (backProject != null){
                ma.setBackUnit(backProject.getUnitName());
                ma.setBackPro(backProject.getProjectName());
                ma.setBackTime(backProject.getCreateTime());
            }
        }else {
            throw new ServiceException("二维码对应机具信息无效");
        }
        return ma;
    }
}
