package cn.com.ty.lift.enterprise.custom.service.impl;

import cn.com.ty.lift.enterprise.custom.dao.entity.PropertyContact;
import cn.com.ty.lift.enterprise.custom.dao.mapper.PropertyContactMapper;
import cn.com.ty.lift.enterprise.custom.service.PropertyContactService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * (甲方联系人)客户联系人表 服务实现类
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-11-11
 */
@Service
@AllArgsConstructor
public class PropertyContactServiceImpl extends ServiceImpl<PropertyContactMapper, PropertyContact> implements PropertyContactService {

    @Transactional
    public RestResponse delete(Long id) {
        PropertyContact byId = this.getById(id);
        if (byId.getStatus() == 2) {
            return RestResponse.fail("有正在服务的关联项目，无法删除");
        }
        Map<String, Long> count = baseMapper.selectProjectCountForPPContactId(byId.getId());
        if (count != null && count.get("count") > 0L) {
            byId.setStatus(2);
            this.updateById(byId);
            return RestResponse.fail("有正在服务的关联项目，无法删除");
        }
        byId.setStatus(0);
        if (this.updateById(byId)) {
            return RestResponse.success(null, "操作成功");
        } else {
            return RestResponse.fail();
        }
    }
}