package com.ruoyi.platform.service.impl;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.domain.AssetIcon;
import com.ruoyi.platform.mapper.AssetIconDao;
import com.ruoyi.platform.service.AssetIconService;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

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

/**
 * (AssetIcon)表服务实现类
 *
 * @author Xidaray
 * @since 2024-03-07 16:02:04
 */
@Service("assetIconService")
public class AssetIconServiceImpl implements AssetIconService {
    @Resource
    private AssetIconDao assetIconDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AssetIcon queryById(Integer id) {
        return this.assetIconDao.queryById(id);
    }


    @Override
    public AssetIcon queryByPath(String path) {
        return this.assetIconDao.queryByPath(path);
    }

    /**
     * 分页查询
     *
     * @param assetIcon 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<AssetIcon> queryByPage(AssetIcon assetIcon, PageRequest pageRequest) {
        long total = this.assetIconDao.count(assetIcon);
        return new PageImpl<>(this.assetIconDao.queryAllByLimit(assetIcon, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param assetIcon 实例对象
     * @return 实例对象
     */
    @Override
    public AssetIcon insert(AssetIcon assetIcon) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        assetIcon.setCreateBy(loginUser.getUsername());
        assetIcon.setUpdateBy(loginUser.getUsername());
        assetIcon.setUpdateTime(new Date());
        assetIcon.setCreateTime(new Date());
        assetIcon.setState(1);
        this.assetIconDao.insert(assetIcon);
        return assetIcon;
    }

    /**
     * 修改数据
     *
     * @param assetIcon 实例对象
     * @return 实例对象
     */
    @Override
    public AssetIcon update(AssetIcon assetIcon) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        assetIcon.setUpdateBy(loginUser.getUsername());
        assetIcon.setUpdateTime(new Date());
        this.assetIconDao.update(assetIcon);
        return this.queryById(assetIcon.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.assetIconDao.deleteById(id) > 0;
    }

    @Override
    public String removeById(Integer id) throws Exception {
        AssetIcon assetIcon = this.assetIconDao.queryById(id);
        if (assetIcon == null){
            throw new Exception("图标不存在");
        }

        //判断权限，只有admin和创建者本身可以删除
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();


        String createdBy = assetIcon.getCreateBy();
        if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){
            throw new Exception("无权限删除该图标");
        }

        assetIcon.setState(0);
        return this.assetIconDao.update(assetIcon)>0?"删除成功":"删除失败";
    }

    @Override
    public List<AssetIcon> queryByName(String name) {
        return this.assetIconDao.queryByName(name);
    }

    @Override
    public List<AssetIcon> queryByCategoryId(Integer categoryId) {
        return this.assetIconDao.queryByCategoryId(categoryId);
    }


}
