package com.eitc.web.controller.saomiaoyi;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.IgnoreReturnResultEncryption;
import com.eitc.common.config.EitcConfig;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.utils.InterfaceVerificationUtil;
import com.eitc.common.utils.file.FileUploadUtils;
import com.eitc.framework.config.ServerConfig;
import com.eitc.patient.domain.ImageRecord;
import com.eitc.patient.service.IImageRecordService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * SaoMiaoYiController
 *
 * @author mtx
 * @date: 2024/09/11 14:37
 */
@RestController
@RequestMapping("/pc/api/scanner")
public class SaoMiaoYiController {

    @Resource
    private RestTemplate restTemplate;
    @Value("${Total.ip}")
    private String fileUrl;
    @Resource
    private IImageRecordService imageRecordService;
    @Resource
    private ServerConfig serverConfig;

    /**
     * 慧视口腔-诊所列表
     */
    @GetMapping(value = "/clinicList")
    @ApiOperation("慧视健康-诊所列表")
    @IgnoreReturnResultEncryption
    public AjaxResult clinicList() {
        // 这个是为了防止恶意请求来做的参数增加
        String saSignStr = InterfaceVerificationUtil.getSaSignStr(new HashMap<>());
        String url = fileUrl + "api/sys/clinict/appGetClinictList?" + saSignStr;
        String object = restTemplate.getForObject(url, String.class);
        return JSONObject.parseObject(object, AjaxResult.class);
    }


    @PostMapping("/uploads")
    @ApiOperation("上传影像")
    @IgnoreReturnResultEncryption
    public AjaxResult uploadFiles(String identificationCard, List<MultipartFile> files) {
        if (StringUtils.isBlank(identificationCard)) {
            return AjaxResult.warn("请输入身份证号");
        }
        try {
            List<ImageRecord> list = new ArrayList<>();
            // 上传文件路径
            String filePath = EitcConfig.getUploadPath();
            // 上传并返回新文件名称
            for (MultipartFile file : files) {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                ImageRecord imageRecord = new ImageRecord();
                imageRecord.setImageUrl(fileName);
                String url = serverConfig.getUrl() + fileName;
                imageRecord.setFullImageUrl(url);
                imageRecord.setIdentificationCard(identificationCard);
                list.add(imageRecord);
            }
            return toAjax(imageRecordService.saveBatch(list));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("查询影像")
    @IgnoreReturnResultEncryption
    public AjaxResult list(ImageRecord imageRecord) {
        List<ImageRecord> list = imageRecordService.list(Wrappers.<ImageRecord>lambdaQuery()
                .eq(StringUtils.isNotBlank(imageRecord.getIdentificationCard()), ImageRecord::getIdentificationCard, imageRecord.getIdentificationCard()));
        return AjaxResult.success(list);
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除影像")
    @IgnoreReturnResultEncryption
    public AjaxResult remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return toAjax(imageRecordService.removeBatchByIds(idList));
    }

    private AjaxResult toAjax(boolean result) {
        return result ? AjaxResult.success() : AjaxResult.error();
    }

}
