package com.zt.plat.module.qms.business.bus.controller.admin;

import static com.zt.plat.framework.common.pojo.CommonResult.error;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationSubmitReqVO;
import org.springframework.validation.annotation.Validated;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustParam;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleEntrustContext;
import com.zt.plat.module.qms.business.bus.service.SampleEntrustService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

@Tag(name = "管理后台 - 样品委托")
@RestController
@RequestMapping("/qms/bus/sample/entrust")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class SampleEntrustController implements BusinessControllerMarker {

    @Resource
    private FlowExecutor flowExecutor;
    
    @Resource
    private SampleEntrustService sampleEntrustService;
        
    @PostMapping("/create")
    @Operation(summary = "创建委托登记")
    public CommonResult<?> create(@Valid @RequestBody SampleEntrustParam sampleEntrustParam) {
    	LiteflowResponse response = sampleEntrustService.create(sampleEntrustParam);
    	if (response.isSuccess()) {
    		SampleEntrustContext sampleEntrustContext = response.getContextBean(SampleEntrustContext.class);
			return success(sampleEntrustContext.getSampleEntrustRegistration());
		} else {
			return error(1_032_100_000, response.getMessage());
		}
    }
    
    @GetMapping("/detail")
    @Operation(summary = "委托登记详情")
    public CommonResult<?> detail(@RequestParam("id") Long id) {
    	BusinessSampleEntrustRegistrationExtendRespVO sampleEntrustRegistration = sampleEntrustService.detail(id);
    	return success(sampleEntrustRegistration);
    }
    
    @PostMapping("/update")
    @Operation(summary = "修改委托登记")
    public CommonResult<?> update(@Valid @RequestBody SampleEntrustParam sampleEntrustParam) {
    	LiteflowResponse response = sampleEntrustService.update(sampleEntrustParam);
    	if (response.isSuccess()) {
    		SampleEntrustContext sampleEntrustContext = response.getContextBean(SampleEntrustContext.class);
			return success(sampleEntrustContext.getSampleEntrustRegistration());
		} else {
			return error(1_032_100_000, response.getMessage());
		}
    }

    @PostMapping("/submit")
    @Operation(summary = "提交托登记")
    public CommonResult<?> submit(@Valid @RequestBody BusinessSampleEntrustRegistrationSubmitReqVO req) {
    	//如果仅勾选了送样，则需要把收样也勾选
    	if (req.getIsSendSample().equals(1) && req.getIsReceiveSample().equals(0)) {
			req.setIsReceiveSample(1);
		}
    	sampleEntrustService.submit(req);
    	return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委托登记")
    @Parameter(name = "id", description = "ID", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
    	sampleEntrustService.delete(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "ID", required = true)
    @Operation(summary = "批量删除委托登记")
    public CommonResult<Boolean> deleteList(@RequestBody BatchDeleteReqVO req) {
    	sampleEntrustService.deleteList(req.getIds());
        return success(true);
    }

}
