package cn.ysk.cashier.service.impl;

import cn.ysk.cashier.dto.shop.TbShopPermissionDto;
import cn.ysk.cashier.mybatis.mapper.TbShopPermissionMapper;
import cn.ysk.cashier.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ysk.cashier.entity.TbShopPermission;
import cn.ysk.cashier.service.TbShopPermissionService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (TbShopPermission)表服务实现类
 *
 * @author makejava
 * @since 2024-09-14 10:53:11
 */
@Service("tbShopPermissionService")
public class TbShopPermissionServiceImpl extends ServiceImpl<TbShopPermissionMapper, TbShopPermission> implements TbShopPermissionService {

    @Override
    public TbShopPermission queryByCode(String code) {
        QueryWrapper<TbShopPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        return this.getOne(queryWrapper);
    }

    @Override
    public ResponseEntity<Object> insert(TbShopPermissionDto permissionDto) {
        // 校验类型和code是否已存在
        if (this.checkTypeAndCodeExist(permissionDto.getType(), permissionDto.getCode(), null)) {
            return ResponseEntity.badRequest().body("类型和code已存在");
        }

        TbShopPermission permission = new TbShopPermission();
        permission.setType(permissionDto.getType());
        permission.setLabel(permissionDto.getLabel());
        permission.setCode(permissionDto.getCode());
        permission.setIsImportant(permissionDto.getIsImportant());
        permission.setSort(permissionDto.getSort());
        permission.setLevel(permissionDto.getLevel());
        permission.setParentId(permissionDto.getParentId());
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());

        this.save(permission);

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

    @Override
    public ResponseEntity<Object> update(TbShopPermissionDto permissionDto) {
        // 检查记录是否存在
        TbShopPermission permission = this.getById(permissionDto.getId());
        if (permission == null) {
            return ResponseEntity.badRequest().body("记录不存在");
        }

        // 校验类型和code是否已存在
        if (this.checkTypeAndCodeExist(permissionDto.getType(), permissionDto.getCode(), permissionDto.getId())) {
            return ResponseEntity.badRequest().body("类型和code已存在");
        }

        if (StringUtils.isNotBlank(permission.getType())) permission.setType(permissionDto.getType());
        if (StringUtils.isNotBlank(permission.getLabel())) permission.setLabel(permissionDto.getLabel());
        if (StringUtils.isNotBlank(permission.getCode())) permission.setCode(permissionDto.getCode());
        if (permissionDto.getIsImportant() != null) permission.setIsImportant(permissionDto.getIsImportant());
        if (permissionDto.getSort() != null) permission.setSort(permissionDto.getSort());
        if (permissionDto.getLevel() != null) permission.setLevel(permissionDto.getLevel());
        if (permissionDto.getParentId() != null) permission.setParentId(permissionDto.getParentId());
        permission.setUpdateTime(new Date());

        this.updateById(permission);

        return ResponseEntity.ok("修改成功");
    }

    @Override
    public ResponseEntity<Object> delete(Integer id) {
        // 检查记录是否存在
        TbShopPermission permission = this.getById(id);
        if (permission == null) {
            return ResponseEntity.badRequest().body("记录不存在");
        }

        this.removeById(id);

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

    @Override
    public List<TbShopPermissionDto> getShopPermissionList() {
        // 查询level为1的权限
        QueryWrapper<TbShopPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 1);
        queryWrapper.orderByAsc("sort");
        List<TbShopPermission> list = this.list(queryWrapper);

        List<TbShopPermissionDto> permissionDtoList = new ArrayList<>();
        // 查询子权限
        for (TbShopPermission permission : list) {
            String jsonString = JSONObject.toJSONString(permission);
            TbShopPermissionDto permissionDto = JSONObject.parseObject(jsonString, TbShopPermissionDto.class);

            QueryWrapper<TbShopPermission> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id", permission.getId());
            wrapper.orderByAsc("sort");
            List<TbShopPermission> children = this.list(wrapper);

            String listStr = JSONObject.toJSONString(children);
            List<TbShopPermissionDto> childrenDto = JSONObject.parseArray(listStr, TbShopPermissionDto.class);
            permissionDto.setChildren(childrenDto);

            permissionDtoList.add(permissionDto);
        }

        return permissionDtoList;
    }

    private boolean checkTypeAndCodeExist(String type, String code, Integer id) {
        QueryWrapper<TbShopPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        queryWrapper.eq("code", code);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return this.count(queryWrapper) > 0;
    }
}

