package cn.ysk.cashier.mybatis.service.impl;

import cn.ysk.cashier.dto.TbMiniAppPagesDto;
import cn.ysk.cashier.mybatis.entity.TbMiniAppPages;
import cn.ysk.cashier.mybatis.mapper.TbMiniAppPagesMapper;
import cn.ysk.cashier.mybatis.service.TbMiniAppPagesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author GYJ
 */
@Service
@RequiredArgsConstructor
public class TbMiniAppPagesServiceImpl extends ServiceImpl<TbMiniAppPagesMapper, TbMiniAppPages> implements TbMiniAppPagesService {
    @Override
    public ResponseEntity<Object> createTbMiniAppPages(TbMiniAppPagesDto pagesDto) {
        if (pagesDto == null) {
            return ResponseEntity.badRequest().body("参数不能为空");
        }
        if (pagesDto.getName() == null || pagesDto.getPath() == null) {
            return ResponseEntity.badRequest().body("页面名称和路径不能为空");
        }
        QueryWrapper<TbMiniAppPages> wrapper = new QueryWrapper<>();
        wrapper.eq("name", pagesDto.getName());
        TbMiniAppPages tbMiniAppPages = baseMapper.selectOne(wrapper);
        if (tbMiniAppPages != null) {
            return ResponseEntity.badRequest().body("页面已存在");
        }

        tbMiniAppPages = new TbMiniAppPages();
        tbMiniAppPages.setName(pagesDto.getName());
        tbMiniAppPages.setPath(pagesDto.getPath());
        tbMiniAppPages.setIcon(pagesDto.getIcon());
        tbMiniAppPages.setSort(pagesDto.getSort());
        tbMiniAppPages.setDescription(pagesDto.getDescription());
        tbMiniAppPages.setStatus(pagesDto.getStatus());
        tbMiniAppPages.setCreateTime(new Date());
        tbMiniAppPages.setUpdateTime(new Date());

        baseMapper.insert(tbMiniAppPages);

        return ResponseEntity.ok().body("新增成功");
    }

    @Override
    public ResponseEntity<Object> updateTbMiniAppPages(TbMiniAppPagesDto pagesDto) {
        TbMiniAppPages appPages = baseMapper.selectById(pagesDto.getId());
        if (appPages == null) {
            return ResponseEntity.badRequest().body("页面不存在");
        }

        if (pagesDto.getName() == null || pagesDto.getPath() == null) {
            return ResponseEntity.badRequest().body("页面名称和路径不能为空");
        }

        if (!appPages.getName().equals(pagesDto.getName())) {
            QueryWrapper<TbMiniAppPages> wrapper = new QueryWrapper<>();
            wrapper.eq("name", pagesDto.getName());
            TbMiniAppPages tbMiniAppPages = baseMapper.selectOne(wrapper);
            if (tbMiniAppPages != null) {
                return ResponseEntity.badRequest().body("页面已存在");
            }
        }

        if (pagesDto.getName() != null) {
            appPages.setName(pagesDto.getName());
        }
        if (pagesDto.getPath() != null) {
            appPages.setPath(pagesDto.getPath());
        }
        if (pagesDto.getIcon() != null) {
            appPages.setIcon(pagesDto.getIcon());
        }
        if (pagesDto.getSort() != null) {
            appPages.setSort(pagesDto.getSort());
        }
        if (pagesDto.getDescription() != null) {
            appPages.setDescription(pagesDto.getDescription());
        }
        if (pagesDto.getStatus() != null) {
            appPages.setStatus(pagesDto.getStatus());
        }
        appPages.setUpdateTime(new Date());

        baseMapper.updateById(appPages);

        return ResponseEntity.ok().body("更新成功");
    }

    @Override
    public ResponseEntity<Object> deleteTbMiniAppPages(Integer pagesId) {
        TbMiniAppPages appPages = baseMapper.selectById(pagesId);
        if (appPages == null) {
            return ResponseEntity.badRequest().body("页面不存在");
        }

        baseMapper.deleteById(pagesId);

        return ResponseEntity.ok().body("删除成功");
    }

    @Override
    public ResponseEntity<Object> getTbMiniAppPages(Map<String, Object> params) {
        QueryWrapper<TbMiniAppPages> wrapper = new QueryWrapper<>();
        if (params.get("status") != null) {
            wrapper.eq("status", params.get("status"));
        }
        wrapper.orderByDesc("sort");
        return ResponseEntity.ok().body(baseMapper.selectList(wrapper));
    }
}
