/*
 * Copyright (c) 2021, orioc and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.bonus.sharedstation.web.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.api.DictDataApi;
import com.bonus.sharedstation.api.vo.DictDataInfoDO;
import com.bonus.sharedstation.app.dto.CleaningServicesDTO;
import com.bonus.sharedstation.app.entity.CleaningServicesEntity;
import com.bonus.sharedstation.app.mapper.CleaningServicesMapper;
import com.bonus.sharedstation.web.service.ICleaningServiceWebService;
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;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Service实现
 *
 * @author hyq
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class CleaningServiceWebServiceImpl extends ServiceImpl<CleaningServicesMapper, CleaningServicesEntity> implements ICleaningServiceWebService
{


    /**
     * Dao
     */
    @Resource
    private CleaningServicesMapper dao;

    @Resource
    private DictDataApi dictDataApi;

    @Override
    @Transactional(readOnly = false)
    public AjaxResult insert(CleaningServicesEntity dto) {
        log.info("新增  入参:{} ", dto);
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        dto.setIsDeleted("0");
        boolean b = save(dto);
        if (!b) {
            return AjaxResult.error("新增清洁服务品类价格失败");
        }
        return AjaxResult.success("新增清洁服务品类价格成功");
    }


    @Override
    @Transactional(readOnly = false)
    public AjaxResult updateInfoById(CleaningServicesEntity dto) {
        log.info("修改 入参: {}", dto);
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = updateById(dto);
        if (!b) {
            return AjaxResult.error("清洁服务品类价格修改失败");
        }
        return AjaxResult.success("清洁服务品类价格修改成功");
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPage(CleaningServicesDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<CleaningServicesEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<CleaningServicesEntity> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        if (StringUtils.isNotEmpty( dto.getServiceName())){
            queryWrapper.like(CleaningServicesEntity::getServiceName,dto.getServiceName());
        }
        if (StringUtils.isNotEmpty( dto.getType())){
            queryWrapper.eq(CleaningServicesEntity::getType,dto.getType());
        }
        queryWrapper
                .eq(CleaningServicesEntity::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CleaningServicesEntity::getCreatedTime);     // 降序排序

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

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPageApp(CleaningServicesDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<CleaningServicesEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<CleaningServicesEntity> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        if (StringUtils.isNotEmpty( dto.getServiceName())){
            queryWrapper.like(CleaningServicesEntity::getServiceName,dto.getServiceName());
        }
        if (StringUtils.isNotEmpty(dto.getType())){
            queryWrapper.eq(CleaningServicesEntity::getType,dto.getType());
        }
        queryWrapper
                .eq(CleaningServicesEntity::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CleaningServicesEntity::getCreatedTime);     // 降序排序

        Page<CleaningServicesEntity> result = dao.selectPage(page, queryWrapper);
        //处理参数问题

        getTypeNameMethd(result);
        return AjaxResult.success(result);
    }

    /**
     * type类型转换
     * @param result
     */
    private void getTypeNameMethd(Page<CleaningServicesEntity> result) {
        //处理参数问题
        DictDataInfoDO dictDataInfoDO = new DictDataInfoDO();
        //字典类型
        dictDataInfoDO.setDataType("equipment_type");
        List<DictDataInfoDO> equipmentTypeList = dictDataApi.getDictionaryInfo("cleaning_type");
        if (StringUtils.isEmpty(equipmentTypeList)) {
            throw new RuntimeException("字典数据为空");
        }
        Map<String, String> map = equipmentTypeList.stream()
                .collect(Collectors.toMap(DictDataInfoDO::getDataCode, DictDataInfoDO::getDataValue));
        List<CleaningServicesEntity> records = result.getRecords();

        if (StringUtils.isNotEmpty(records)) {
            records.stream().forEach(emergencyEquipment -> {
                if (map.containsKey(emergencyEquipment.getType())) {
                    emergencyEquipment.setTypeName(map.get(emergencyEquipment.getType()));
                }

            });
        }
    }

    @Override
    @Transactional(readOnly = false)
    public AjaxResult delete(CleaningServicesEntity dto) {
        return AjaxResult.success(dao.deleteById(dto.getId()));
    }

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @Override
    public AjaxResult selectAll(CleaningServicesEntity dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<CleaningServicesEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .eq(CleaningServicesEntity::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(CleaningServicesEntity::getCreatedTime);     // 降序排序

        List<CleaningServicesEntity> result = dao.selectList(queryWrapper);
        return AjaxResult.success(result);
    }


    /**
     * 详情
     *
     * @param
     * @return
     */
    @Override
    public AjaxResult selectInfoById(CleaningServicesEntity dto) {
        CleaningServicesEntity result = dao.selectById(dto.getId());
        if (StringUtils.isNull(result)) {
            return AjaxResult.error("不存在");
        }
        //处理参数问题
        return AjaxResult.success(result);
    }
}
