package digital.laboratory.platform.imr.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.constant.CommonConstants;
import digital.laboratory.platform.common.core.exception.ValidateCodeException;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.feign.RemoteTemplate2htmlService;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.imr.convert.DrugCaseInfoConvert;
import digital.laboratory.platform.imr.dto.DrugDepotsDTO;
import digital.laboratory.platform.imr.dto.DrugMaterialInfoDTO;
import digital.laboratory.platform.imr.dto.InRepositoryDTO;
import digital.laboratory.platform.imr.entity.DrugCaseInfo;
import digital.laboratory.platform.imr.entity.DrugMaterialInfo;
import digital.laboratory.platform.imr.query.DrugDepotsQuery;
import digital.laboratory.platform.imr.service.CommonFeignService;
import digital.laboratory.platform.imr.service.DrugCaseInfoService;
import digital.laboratory.platform.imr.service.DrugMaterialInfoService;
import digital.laboratory.platform.imr.vo.DrugCaseInfoVO;
import digital.laboratory.platform.imr.vo.DrugMaterialInfoVO;
import digital.laboratory.platform.imr.vo.OutSampleVO;
import digital.laboratory.platform.sys.entity.Drug;
import digital.laboratory.platform.sys.feign.RemoteOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.ValidationException;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/drugDepots" )
@Api(value = "DrugDepotsController", tags = "毒品库信息管理接口")
public class DrugDepotsController {

    @Resource
    private DrugCaseInfoService drugCaseInfoService;

    @Resource
    private DrugMaterialInfoService drugMaterialInfoService;


    @ApiOperation(value = "新增毒品案件信息")
    @PostMapping("/drugCase/save")
    public R drugCaseSave(@RequestBody @Valid DrugDepotsDTO dto) {
        DrugCaseInfo save = null;
        try {
            save = drugCaseInfoService.drugCaseSave(dto);
        } catch (ValidateCodeException e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
        return R.ok(save);
    }

    @ApiOperation(value = "新增毒品案件信息和毒品检材信息")
    @PostMapping("/save")
    public R save(@RequestBody @Valid DrugDepotsDTO dto) {
        List<DrugMaterialInfoVO> save = drugCaseInfoService.save(dto);
        return R.ok(save);
    }

    @ApiOperation(value = "复制毒品检材信息")
    @PostMapping("/copy")
    public R copy(@RequestBody DrugMaterialInfoDTO dto) {
        DrugMaterialInfoVO copy = drugMaterialInfoService.copy(dto);
        return R.ok(copy);
    }

    @ApiOperation(value = "毒品案件信息和毒品检材信息分页接口")
    @PostMapping("/drugMaterial/page")
    public R<IPage<DrugMaterialInfoVO>> drugMaterialPage(@RequestBody DrugDepotsQuery query) {
        IPage<DrugMaterialInfoVO> page = drugMaterialInfoService.page(query);
        return R.ok(page);
    }

    @ApiOperation(value = "毒品案件信息分页接口")
    @PostMapping("/drugCase/page")
    public R<IPage<DrugCaseInfoVO>> drugCasePage(@RequestBody DrugDepotsQuery query) {
        IPage<DrugCaseInfoVO> drugCaseInfoVOPage = drugCaseInfoService.getDrugCaseInfoVOPage(query);
        return R.ok(drugCaseInfoVOPage);
    }

    @ApiOperation("删除毒品检材信息")
    @PostMapping("/delete/drugMaterial")
    public R deleteDrugMaterial(@RequestBody List<String> ids) {
        boolean success = false;
        try {
            success = drugMaterialInfoService.delete(ids);
        } catch (ValidationException e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
        return success ? R.ok(true, "删除成功！") : R.failed(false, "删除失败！");
    }

    @ApiOperation(value = "根据模板生成样本标签的 html, 供 qz 打印使用", notes = "根据模板生成样本标签的 html, 供 qz 打印使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检材标识", required = true, paramType = "path")
    })
    @GetMapping("/drug_label/{id}")
    public String getDrugLabelHtml(@PathVariable(value = "id") String id)  {
        return drugMaterialInfoService.buildDrugLabelHtml(id);
    }

    @ApiOperation(value = "根据毒品编号录入仓库(入库)", notes = "根据样本编号录入仓库(入库)")
    @PostMapping("/drugMaterialPutInRepository")
    public R drugMaterialPutInRepository(@RequestBody List<InRepositoryDTO> sample, HttpServletRequest theHttpServletRequest) {
        Principal principal = theHttpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

        if (CollUtil.isEmpty(sample)) {
            throw new RuntimeException("入库样本编号不能为空");
        }

        List<OutSampleVO> vo = drugMaterialInfoService.drugMaterialPutInRepository(sample, dlpUser);
        if (vo != null) {
            return R.ok(vo, "样本入库成功,本次入库样本数为" + sample.size() + "份");
        } else {
            return R.failed("入库失败");
        }
    }
}
