package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.CollectCostPatients;
import com.eitc.patient.domain.CollectCostPatientsDetails;
import com.eitc.patient.service.ICollectCostPatientsDetailsService;
import com.eitc.patient.service.ICollectCostPatientsService;
import com.eitc.patient.vo.CollectCostDetailsVO;
import com.eitc.patient.vo.CollectCostPatientsDetailsListVO;
import com.eitc.patient.vo.CollectCostPatientsDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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 javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CollectCostPatientsController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "患者收费信息")
@RestController
@RequestMapping("/collect-cost-patients")
public class CollectCostPatientsController extends BaseController {

    @Resource
    private ICollectCostPatientsService collectCostPatientsService;
    @Resource
    private ICollectCostPatientsDetailsService collectCostPatientsDetailsService;

    /**
     * 根据患者id获取收费列表
     */
    @GetMapping("/listByPatientId")
    @ApiOperation("查询患者收费信息")
    public AjaxResult listByPatientId(@RequestParam(name = "patientId",defaultValue = "0") String patientId) {
        LambdaQueryWrapper<CollectCostPatients> eq = Wrappers.<CollectCostPatients>lambdaQuery()
                .eq(CollectCostPatients::getPatientId, patientId)
                .orderByDesc(CollectCostPatients::getCreateDate);

        return success(collectCostPatientsService.list(eq));
    }

    /**
     * 根据收费列表id获取收费详情
     */
    @GetMapping("/listToDetails")
    @ApiOperation("查询患者收费信息")
    public AjaxResult listToDetails(@RequestParam(name = "collectCostPatientsId") String collectCostPatientsId) {
        CollectCostPatients collectCostPatients = collectCostPatientsService.getById(collectCostPatientsId);
        CollectCostPatientsDetailsVO vo = new CollectCostPatientsDetailsVO();
        vo.setCollectCostPatientsId(collectCostPatients.getId());
        vo.setCostStatus(collectCostPatients.getCostStatus());
        vo.setReceivableCost(collectCostPatients.getReceivableCost());
        vo.setNetReceiptsCost(collectCostPatients.getNetReceiptsCost());

        LambdaQueryWrapper<CollectCostPatientsDetails> eq = Wrappers.<CollectCostPatientsDetails>lambdaQuery()
                .eq(CollectCostPatientsDetails::getCollectCostPatientsId, collectCostPatientsId)
                .orderByDesc(CollectCostPatientsDetails::getCreateDate);
        List<CollectCostPatientsDetails> list = collectCostPatientsDetailsService.list(eq);
        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, List<CollectCostPatientsDetails>> collect = list.stream().collect(Collectors.groupingBy(CollectCostPatientsDetails::getToothPosition));
            List<CollectCostPatientsDetailsListVO> listVOS = new ArrayList<>();
            for (String tooth : collect.keySet()) {
                CollectCostPatientsDetailsListVO listVO = new CollectCostPatientsDetailsListVO();
                listVO.setToothPosition(tooth);
                List<CollectCostPatientsDetails> detailsList = collect.get(tooth);
                List<CollectCostDetailsVO> detailsVOS = detailsList.stream().map(li -> {
                    CollectCostDetailsVO detailsVO = new CollectCostDetailsVO();
                    BeanUtils.copyProperties(li, detailsVO);
                    return detailsVO;
                }).collect(Collectors.toList());
                listVO.setCollectCostDetailsVOList(detailsVOS);
                listVOS.add(listVO);
            }
            vo.setListVO(listVOS);
        }
        return success(vo);
    }

    /**
     * 编辑收费详情
     */
    @Log(title = "编辑收费详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑收费详情")
    public AjaxResult edit(@Valid @RequestBody CollectCostPatientsDetailsVO vo) {
        collectCostPatientsDetailsService.edit(vo);
        return success();
    }

    @Log(title = "收费", businessType = BusinessType.UPDATE)
    @PostMapping("/collectCost")
    @ApiOperation("收费")
    public AjaxResult collectCost(@Valid @RequestBody CollectCostPatients entry) {
        collectCostPatientsService.collectCost(entry);
        return success();
    }

}
