package digital.laboratory.platform.entrustment.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.log.annotation.SysLog;
import digital.laboratory.platform.entrustment.entity.Entrustment;
import digital.laboratory.platform.entrustment.entity.Identification;
import digital.laboratory.platform.entrustment.service.EntrustmentService;
import digital.laboratory.platform.entrustment.service.IdentificationService;
import digital.laboratory.platform.entrustment.vo.IdentificationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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


/**
 * 鉴定表,一个鉴定可能会有多个委托
 *
 * @author Zhang Xiaolong created at  2022-07-15
 * @describe 鉴定表,一个鉴定可能会有多个委托 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustment/identification" )
@Api(value = "identification", tags = "006-鉴定表,一个鉴定可能会有多个委托管理")
public class IdentificationController {

    private final  IdentificationService identificationService;

	@Resource
	private EntrustmentService entrustmentService;

	/**
	 * 通过id查询鉴定表,一个鉴定可能会有多个委托
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
//	@PreAuthorize("@pms.hasPermission('entrustment_identification_get')" )
	public R getById(@PathVariable("id" ) String id) {
		return R.ok(identificationService.getById(id));
	}


	/**
	 * 废弃了xxxx
	 * 根据 caseId 取 IdentificationVO 列表, 每个 Entrustment 一行
	 * @param caseId
	 * @return R
	 */
	@ApiOperation(value = "根据 caseId 取 IdentificationVO 列表, 每个 Entrustment 一行", notes = "根据 caseId 取 IdentificationVO 列表, 每个 Entrustment 一行")
	@GetMapping("/vo_list_by_case_id" )
//	@PreAuthorize("@pms.hasPermission('entrustment_identification_get')" )
	public R<List<IdentificationVO>> getIdentificationVOList(String caseId) {
		return R.ok(identificationService.getIdentificationVOList(caseId));
	}

	/**
	 * 根据 caseId 取 Identification 列表, 供新建的补充委托选择老的 Identification(鉴定), 以使新建的委托作为老的鉴定的一部分
	 * @param caseId
	 * @return
	 */
	@ApiOperation(value = "根据 caseId 取 Identification 列表, 供新建的补充委托选择老的 Identification(鉴定), 以使新建的委托作为老的鉴定的一部分",
			notes = "根据 caseId 取 Identification 列表, 供新建的补充委托选择老的 Identification(鉴定), 以使新建的委托作为老的鉴定的一部分")
	@GetMapping("/list_by_case" )
//    @PreAuthorize("@pms.hasPermission('entrustment_identification_get')" )
	public R<List<Entrustment>> getIdentificationListByCaseId(String caseId) {
		//return R.ok(identificationService.getIdentificationListByCaseId(caseId));
		//求出case 下的委托，并且这些委托是受理后的
		List<Entrustment> entrustmentList=entrustmentService.list(Wrappers.<Entrustment>query()
				.eq("case_id",caseId)
				.ge("status",8)
				.le("status",90));
		return R.ok(entrustmentList,"获取数据成功");

	}


	/**
     * 分页查询
     * @param page 分页对象
     * @param identification 鉴定表,一个鉴定可能会有多个委托
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('entrustment_identification_get')" )
    public R getIdentificationPage(Page page, Identification identification) {
        return R.ok(identificationService.page(page, Wrappers.query(identification)));
    }

    /**
     * 新增鉴定表,一个鉴定可能会有多个委托
     * @param identification 鉴定表,一个鉴定可能会有多个委托
     * @return R
     */
    @ApiOperation(value = "新增鉴定表,一个鉴定可能会有多个委托", notes = "新增鉴定表,一个鉴定可能会有多个委托")
    @SysLog("新增鉴定表,一个鉴定可能会有多个委托" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('entrustment_identification_add')" )
    public R postAddObject(@RequestBody Identification identification) {
		identification.setId(IdWorker.get32UUID().toUpperCase());

	    if (identificationService.save(identification)) {
		    return R.ok(identification, "新增鉴定成功");
	    }
	    else {
		    return R.failed(identification, "新增鉴定失败");
	    }

    }

    /**
     * 修改鉴定表,一个鉴定可能会有多个委托
     * @param identification 鉴定表,一个鉴定可能会有多个委托
     * @return R
     */
    @ApiOperation(value = "修改鉴定表,一个鉴定可能会有多个委托", notes = "修改鉴定表,一个鉴定可能会有多个委托")
    @SysLog("修改鉴定表,一个鉴定可能会有多个委托" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('entrustment_identification_edit')" )
    public R putUpdateById(@RequestBody Identification identification) {
        return R.ok(identificationService.updateById(identification));
    }



    /**
     * 通过id删除鉴定表,一个鉴定可能会有多个委托
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除鉴定表,一个鉴定可能会有多个委托", notes = "通过id删除鉴定表,一个鉴定可能会有多个委托")
    @SysLog("通过id删除鉴定表,一个鉴定可能会有多个委托" )
    @DeleteMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('entrustment_identification_del')" )
    public R deleteById(@PathVariable String id) {
        return R.ok(identificationService.removeById(id));
    }

	@GetMapping("/create10")
	public R create10() {
		int foo = 10;
		for (int i = 0; i < foo; i++) {
			Identification identification = new Identification();
			identification.setId(IdWorker.get32UUID().toUpperCase());
			String idNo = identificationService.getNewIdentificationNo();
			System.out.printf("新的 id=%s identificationNo = %s\n", identification.getId(), idNo);
			identification.setIdentificationNo(idNo);
			identification.setStatus(0);
			identificationService.save(identification);
		}
		return R.ok("Create 10 Identification samples OK");
	}


}
