package digital.laboratory.platform.imr.controller;


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.util.R;
import digital.laboratory.platform.imr.convert.DrugUsageRecordConvert;
import digital.laboratory.platform.imr.dto.DrugUsageRecordDTO;
import digital.laboratory.platform.imr.entity.DrugMaterialInfo;
import digital.laboratory.platform.imr.entity.DrugUsageRecord;
import digital.laboratory.platform.imr.enums.DrugMaterialStatus;
import digital.laboratory.platform.imr.mapper.DrugMaterialInfoMapper;
import digital.laboratory.platform.imr.query.BaseQuery;
import digital.laboratory.platform.imr.service.CommonFeignService;
import digital.laboratory.platform.imr.service.DrugUsageRecordService;
import digital.laboratory.platform.imr.vo.DrugMaterialInfoVO;
import digital.laboratory.platform.imr.vo.DrugUsageRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/drugUsageRecord")
@Api(value = "DrugUsageRecordController", tags = "毒品检材的领用归还记录表相关接口")
public class DrugUsageRecordController {

    @Resource
    private DrugUsageRecordService drugUsageRecordService;

    @Resource
    private DrugMaterialInfoMapper drugMaterialInfoMapper;

    @Resource
    private CommonFeignService commonFeignService;

    @ApiOperation("毒品检材领用出库")
    @PostMapping("/outbound")
    public R outbound(@RequestBody @Valid DrugUsageRecordDTO dto) {
        boolean success = drugUsageRecordService.outbound(dto);
        return R.ok(success);
    }

    @ApiOperation("毒品检材领用记录分页列表")
    @PostMapping("/page")
    public R<IPage<DrugUsageRecordVO>> page(@RequestBody BaseQuery query) {
        String keywords = query.getKeywords();
        IPage<DrugUsageRecord> page = drugUsageRecordService.page(
                new Page<>(query.getCurrent(), query.getSize()),
                Wrappers.<DrugUsageRecord>lambdaQuery()
                        .and(StrUtil.isNotBlank(keywords),
                                wrapper -> wrapper
                                        .like(DrugUsageRecord::getUsageName, keywords)
                                        .or()
                                        .like(DrugUsageRecord::getApplicantName, keywords)
                                        .or()
                                        .like(DrugUsageRecord::getUsagePurpose, keywords)
                        )
                        .ge(query.getStartDate() != null, DrugUsageRecord::getUsageDate, query.getStartDate())
                        .le(query.getEndDate() != null, DrugUsageRecord::getUsageDate, query.getEndDate()));
        IPage<DrugUsageRecordVO> voiPage = new Page<>();
        BeanUtils.copyProperties(page, voiPage, "records");
        voiPage.setRecords(DrugUsageRecordConvert.entityToVOList(page.getRecords()));
        return R.ok(voiPage);
    }

    @ApiOperation("毒品检材领用记录详情")
    @GetMapping("/details")
    public R details(@RequestParam("id") String id) {
        DrugUsageRecord drugUsageRecord = drugUsageRecordService.getById(id);
        DrugUsageRecordVO drugUsageRecordVO = DrugUsageRecordConvert.entityToVO(drugUsageRecord);
        List<DrugMaterialInfoVO> drugMaterialInfoVOS = drugMaterialInfoMapper.getDrugMaterialVO(Wrappers.<DrugMaterialInfo>query()
                .in("dm.id", drugUsageRecordVO.getDrugIds()));
        drugMaterialInfoVOS.parallelStream().forEach(item -> {
            item.setHandingOverOrgName(commonFeignService.remoteGetSysOrg(item.getHandingOverOrg()).getName());
            item.setStatusName(DrugMaterialStatus.fromStatus(item.getStatus()).getDesc());
        });
        drugUsageRecordVO.setDrugMaterialInfoVOList(drugMaterialInfoVOS);
        return R.ok(drugUsageRecordVO);
    }

}
