/*
 * Copyright ©2023 CJB-CNIT Team. All rights reserved
 */
package com.openhis.web.basedatamanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.core.common.utils.MessageUtils;
import com.openhis.administration.domain.Frequency;
import com.openhis.administration.service.IFrequencyService;
import com.openhis.common.constant.PromptMsgConstant;
import com.openhis.web.basedatamanage.appservice.IOrganizationAppService;
import com.openhis.web.basedatamanage.dto.FrequencyDto;
import com.openhis.web.basedatamanage.dto.OrgLocQueryDto;
import com.openhis.web.basedatamanage.dto.OrganizationDto;
import com.openhis.web.basedatamanage.dto.OrganizationInitDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * 频次信息管理controller
 *
 */
@RestController
@RequestMapping("/personalization/frequency")
@Slf4j
@AllArgsConstructor
public class FrequencyController {

    private final IFrequencyService frequencyService;

    /**
     * 频次详情分页列表
     *
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @return 机构分页列表
     */
    @GetMapping(value = "/getPageList")
    public R<?> getOrganizationPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, String code,HttpServletRequest request) {
        Page<Frequency> pageList= frequencyService.getPageList(pageNo, pageSize, code,request) ;
        return R.ok(pageList,
            MessageUtils.createMessage(PromptMsgConstant.Common.M00009, new Object[] {"频次详情信息"}));
    }


    @PostMapping("/save")
    public R<?> addOrEditFrequency(@Validated @RequestBody FrequencyDto frequencyDto) {
        // 创建 Frequency 对象
        Frequency frequency = new Frequency();
        // 使用 BeanUtils 拷贝属性
        BeanUtils.copyProperties(frequencyDto, frequency);
        // 获取并转换 dayTimes 字段
        List<String> dayTimesList = frequencyDto.getDayTimes();
        if (dayTimesList != null && !dayTimesList.isEmpty()) {
            Collections.sort(dayTimesList);
            String dayTimesStr = String.join(",", dayTimesList);  // 转换为逗号分隔的字符串
            frequency.setDayTimes(dayTimesStr);  // 将转换后的字符串设置到 Frequency 对象
        }

        return R.ok(frequencyService.saveOrUpdate(frequency, new LambdaQueryWrapper<Frequency>().eq(Frequency::getRateCode, frequency.getRateCode())),MessageUtils.createMessage(PromptMsgConstant.Common.M00002, new Object[] {"保存频次详情"}));
    }

    @DeleteMapping("{id}")
    public R<?> deleteFrequency(@PathVariable Long id) {
        return R.ok(frequencyService.removeById(id),MessageUtils.createMessage(PromptMsgConstant.Common.M00003, new Object[] {"删除频次详情"}));
    }


}
