package com.xueyi.system.resource.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import com.xueyi.file.api.domain.SysFile;
import com.xueyi.file.api.feign.RemoteFileService;
import com.xueyi.system.api.resource.domain.po.DmResourcesPo;
import com.xueyi.system.resource.domain.dto.DmResourcesDto;
import com.xueyi.system.resource.domain.query.DmResourcesQuery;
import com.xueyi.system.resource.manager.IDmResourcesManager;
import com.xueyi.system.resource.mapper.DmResourcesMapper;
import com.xueyi.system.resource.service.IDmResourcesService;
import com.xueyi.system.utils.common.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 静态资源管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class DmResourcesServiceImpl extends BaseServiceImpl<DmResourcesQuery, DmResourcesDto, IDmResourcesManager> implements IDmResourcesService {

    @Autowired
    DmResourcesMapper dmResourcesMapper;

    @Autowired
    private RemoteFileService fileService;

    @Autowired
    private FaceServiceImpl faceService;

    @Autowired
    private ImageUtil imageUtil;

    /**
     * 查询静态资源对象列表 | 数据权限
     *
     * @param dmResources 静态资源对象
     * @return 静态资源对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmResourcesMapper"})
    public List<DmResourcesDto> selectListScope(DmResourcesQuery dmResources) {
        return baseManager.selectList(dmResources);
    }

    @Override
    public Long addOne(DmResourcesPo dmResourcesPo) {
        return dmResourcesMapper.insertOne(dmResourcesPo);
    }

    @Override
    public Long initAdd(DmResourcesPo po) {
        return this.addOne(po);
    }

    @Override
    public R addBase64Img(String base64Img, String name) {
        DmResourcesDto dmResourcesDto = new DmResourcesDto();
        if (StringUtils.isNotEmpty(base64Img)) {
            String imgBase64 = base64Img;
            R<SysFile> fileResult = fileService.uploadFace(imageUtil.base64ToMultipartFile(imgBase64));
            if (ObjectUtil.isNull(fileResult) || ObjectUtil.isNull(fileResult.getData()))
                return R.fail(fileResult.getMsg());
            String url = fileResult.getData().getUrl();

            dmResourcesDto.setType(DmResourcesDto.TYPE_PIC);
            dmResourcesDto.setUrl(url);
            dmResourcesDto.setName(name);

            com.alibaba.fastjson.JSONObject json = faceService.getFaceExtraction("", "", imgBase64);

            if (0!=json.getInteger("status")) {
                return R.fail(json.getString("errMsg"));
            } else {
                JSONArray obj = json.getJSONArray("result").getJSONObject(0).getJSONArray("faces").getJSONObject(0).getJSONArray("feature");
                dmResourcesDto.setFeature(obj.toJSONString());
            }

            addOne(dmResourcesDto);

        }
        return R.ok(dmResourcesDto);
    }
}