package com.cyl.h5.service;

import com.cyl.manager.ums.domain.MemberDoorLock;
import com.cyl.manager.ums.mapper.MemberDoorLockMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 门锁信息Service接口
 * 
 * @author daixiande
 */
@Service
public class H5MemberDoorLockService {

    @Autowired
    private MemberDoorLockMapper memberDoorLockMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    public MemberDoorLock selectMemberDoorLockById(Long id) {
        return memberDoorLockMapper.selectById(id);
    }

    public List<MemberDoorLock> selectMemberDoorLockList(MemberDoorLock memberDoorLock) {
        List<MemberDoorLock>  list =  memberDoorLockMapper.selectList(memberDoorLock);
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        for (MemberDoorLock doorLock : list) {
            doorLock.setHouseType(sysDictDataService.selectDictLabel("house_type",doorLock.getHouseType()));
            doorLock.setLockType(sysDictDataService.selectDictLabel("lock_type",doorLock.getLockType()));
            doorLock.setKeyHandover(sysDictDataService.selectDictLabel("Key_handover",doorLock.getKeyHandover()));
        }
        return list;
    }

    public int insertMemberDoorLock(MemberDoorLock memberDoorLock) {
        deleteMemberDoorLockByOpenId(memberDoorLock.getMemberId());
        memberDoorLock.setCreateTime(LocalDateTime.now());
        memberDoorLock.setCreateBy(memberDoorLock.getMemberId());
        return memberDoorLockMapper.insert(memberDoorLock);
    }

    private void deleteMemberDoorLockByOpenId(Long memberId) {
        memberDoorLockMapper.deleteMemberDoorLockByMemberId(memberId);
    }

    public int updateMemberDoorLock(MemberDoorLock memberDoorLock) {
        memberDoorLock.setUpdateTime(LocalDateTime.now());
        memberDoorLock.setUpdateBy(memberDoorLock.getMemberId());
        return memberDoorLockMapper.update(memberDoorLock);
    }

    public int deleteMemberDoorLockByIds(Long[] ids) {
        return memberDoorLockMapper.deleteByIds(ids);
    }

    public int deleteMemberDoorLockById(Long id) {
        return memberDoorLockMapper.deleteById(id);
    }

    public MemberDoorLock selectMemberDoorLockByOpenId(Long memberId) {
        return memberDoorLockMapper.selectByMemberId(memberId);
    }
}
