/*
 * Copyright (c) 2021, orioc and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.bonus.sharedstation.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.sharedstation.app.service.IPropertyServiceService;
import com.bonus.sharedstation.web.domain.PropertyServiceDTO;
import com.bonus.sharedstation.web.dto.PropertyPageDTO;
import com.bonus.sharedstation.web.mapper.PropertyServiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Service实现
 *
 * @author 云归
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class PropertyServiceServiceImpl extends ServiceImpl<PropertyServiceMapper, PropertyServiceDTO> implements IPropertyServiceService {

    @Resource
    private PropertyServiceMapper propertyServiceMapper;

    @Override
    public AjaxResult insert(PropertyServiceDTO dto) {
        //得到登录人
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = save(dto);
        if (!b) {
            return AjaxResult.error("新增物业服务失败");
        }
        return AjaxResult.success("新增物业服务成功");
    }

    @Override
    public AjaxResult updateInfoById(PropertyServiceDTO dto) {
        log.info("修改 入参: {}", dto);
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = updateById(dto);
        if (!b) {
            return AjaxResult.error("物业服务修改失败");
        }
        return AjaxResult.success("物业服务修改成功");
    }

    @Override
    public AjaxResult getListPage(PropertyPageDTO dto) {
        LambdaQueryWrapper<PropertyServiceDTO> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<PropertyServiceDTO> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(PropertyServiceDTO::getServiceType, dto.getServiceType())
                .eq(PropertyServiceDTO::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(PropertyServiceDTO::getCreatedTime);

        Page<PropertyServiceDTO> result = propertyServiceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getListPageApp(PropertyPageDTO dto) {
        LambdaQueryWrapper<PropertyServiceDTO> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<PropertyServiceDTO> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(PropertyServiceDTO::getServiceType, dto.getServiceType())
                .eq(PropertyServiceDTO::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(PropertyServiceDTO::getCreatedTime);

        Page<PropertyServiceDTO> result = propertyServiceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delete(PropertyServiceDTO emergencyEquipment) {
        return AjaxResult.success(propertyServiceMapper.deleteById(emergencyEquipment.getId()));
    }

    @Override
    public AjaxResult selectAll(PropertyServiceDTO emergencyEquipment) {
        LambdaQueryWrapper<PropertyServiceDTO> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .like(PropertyServiceDTO::getProviderName, emergencyEquipment.getProviderName())
                .eq(PropertyServiceDTO::getIsDeleted, emergencyEquipment.getIsDeleted())
                .orderByDesc(PropertyServiceDTO::getCreatedTime);

        List<PropertyServiceDTO> result = propertyServiceMapper.selectList(queryWrapper);
        return AjaxResult.success(result);
    }


    @Override
    public AjaxResult selectInfoById(PropertyServiceDTO emergencyEquipment) {
        PropertyServiceDTO result = propertyServiceMapper.selectById(emergencyEquipment.getId());
        if (StringUtils.isNull(result)) {
            return AjaxResult.error("此服务不存在");
        }
        //处理参数问题
        return AjaxResult.success(result);
    }
}
