package com.openhis.web.check.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.core.domain.R;
import com.openhis.check.domain.CheckMethod;
import com.openhis.check.domain.CheckPackage;
import com.openhis.check.domain.CheckPart;
import com.openhis.check.domain.CheckType;
import com.openhis.check.service.ICheckMethodService;
import com.openhis.check.service.ICheckPackageService;
import com.openhis.check.service.ICheckPartService;
import com.openhis.check.service.ICheckTypeService;
import com.openhis.web.check.appservice.ICheckPackageAppService;
import com.openhis.web.check.dto.CheckPackageDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

/**
 * 检查类型管理Controller
 *
 * @author system
 * @date 2025-07-22
 * @updated 2025-11-26 - 增加套餐设置相关接口
 */
@RestController
@RequestMapping({"/system/check-type", "/system"})
@Slf4j
@AllArgsConstructor
public class CheckTypeController extends BaseController {

    private final ICheckTypeService checkTypeService;
    private final ICheckMethodService checkMethodService;
    private final ICheckPartService checkPartService;
    private final ICheckPackageService checkPackageService;
    private final ICheckPackageAppService checkPackageAppService;

    /**
     * 获取检查类型列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<CheckType> list = checkTypeService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取检查方法列表
     */
    @GetMapping({"/method/list", "/check-method/list"})
    public AjaxResult methodList() {
        List<CheckMethod> list = checkMethodService.list();
        return AjaxResult.success(list);
    }
    
    /**
     * 获取检查部位列表
     */
    @GetMapping({"/part/list", "/check-part/list"})
    public AjaxResult partList() {
        List<CheckPart> list = checkPartService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取检查套餐列表（支持分页和筛选）
     */
    @GetMapping({"/package/list", "/check-package/list"})
    public AjaxResult packageList(
            @RequestParam(required = false) Integer pageNo,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String organization,
            @RequestParam(required = false) String packageName,
            @RequestParam(required = false) String packageLevel,
            @RequestParam(required = false) String packageType,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String user,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        
        LambdaQueryWrapper<CheckPackage> wrapper = new LambdaQueryWrapper<>();
        
        // 添加筛选条件
        if (organization != null && !organization.isEmpty()) {
            wrapper.eq(CheckPackage::getOrganization, organization);
        }
        if (packageName != null && !packageName.isEmpty()) {
            wrapper.like(CheckPackage::getPackageName, packageName);
        }
        if (packageLevel != null && !packageLevel.isEmpty()) {
            wrapper.eq(CheckPackage::getPackageLevel, packageLevel);
        }
        if (packageType != null && !packageType.isEmpty()) {
            wrapper.eq(CheckPackage::getPackageType, packageType);
        }
        if (department != null && !department.isEmpty()) {
            wrapper.like(CheckPackage::getDepartment, department);
        }
        if (user != null && !user.isEmpty()) {
            wrapper.like(CheckPackage::getUser, user);
        }
        if (startDate != null && !startDate.isEmpty()) {
            wrapper.ge(CheckPackage::getMaintainDate, LocalDate.parse(startDate));
        }
        if (endDate != null && !endDate.isEmpty()) {
            wrapper.le(CheckPackage::getMaintainDate, LocalDate.parse(endDate));
        }
        
        // 按更新时间倒序排列
        wrapper.orderByDesc(CheckPackage::getUpdateTime);
        
        // 如果需要分页
        if (pageNo != null && pageSize != null) {
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<CheckPackage> page = 
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNo, pageSize);
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<CheckPackage> result = 
                checkPackageService.page(page, wrapper);
            return AjaxResult.success(result);
        } else {
            List<CheckPackage> list = checkPackageService.list(wrapper);
        return AjaxResult.success(list);
        }
    }

    /**
     * 新增检查类型
     */
    @PostMapping
    public AjaxResult add(@RequestBody CheckType checkType) {
        return toAjax(checkTypeService.save(checkType));
    }

    /**
     * 修改检查类型
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CheckType checkType) {
        return toAjax(checkTypeService.updateById(checkType));
    }

    /**
     * 删除检查类型
     */
    @DeleteMapping("/{checkTypeId}")
    public AjaxResult remove(@PathVariable Long checkTypeId) {
        return toAjax(checkTypeService.removeById(checkTypeId));
    }

    /**
     * 根据ID获取检查套餐详情
     */
    @GetMapping({"/package/{id}", "/check-package/{id}"})
    public R<?> getCheckPackageById(@PathVariable Long id) {
        return checkPackageAppService.getCheckPackageById(id);
    }

    /**
     * 新增检查套餐
     */
    @PostMapping({"/package", "/check-package"})
    public R<?> addCheckPackage(@Valid @RequestBody CheckPackageDto checkPackageDto) {
        return checkPackageAppService.addCheckPackage(checkPackageDto);
    }

    /**
     * 更新检查套餐
     */
    @PutMapping({"/package", "/check-package"})
    public R<?> updateCheckPackage(@Valid @RequestBody CheckPackageDto checkPackageDto) {
        return checkPackageAppService.updateCheckPackage(checkPackageDto);
    }

    /**
     * 删除检查套餐
     */
    @DeleteMapping({"/package/{id}", "/check-package/{id}"})
    public R<?> deleteCheckPackage(@PathVariable Long id) {
        return checkPackageAppService.deleteCheckPackage(id);
    }
}
