package com.ruoyi.web.work.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Introduction;
import com.ruoyi.web.work.service.IIntroductionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医院介绍
 *
 * @author lsw
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/work/introduction")
public class IntroductionController extends BaseController {
    @Autowired
    private IIntroductionService introductionService;

    @PreAuthorize("@ss.hasPermi('work:introduction:list')")
    @GetMapping("/list")
    public TableDataInfo list(Introduction introduction) {
        startPage();
        List<Introduction> list = introductionService.selectList(introduction);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:introduction:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(introductionService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:introduction:add')")
    @Log(title = "医院介绍", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Introduction introduction) {
        return toAjax(introductionService.save(introduction));
    }

    @PreAuthorize("@ss.hasPermi('work:introduction:edit')")
    @Log(title = "医院介绍", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Introduction introduction) {
        return toAjax(introductionService.updateById(introduction));
    }

    @PreAuthorize("@ss.hasPermi('work:introduction:remove')")
    @Log(title = "医院介绍", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(introductionService.removeByIds(Arrays.asList(ids)));
    }

    @PreAuthorize("@ss.hasPermi('work:introduction:updateStatus')")
    @Log(title = "医院介绍更新状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatusedit(@RequestBody Introduction introduction) {


        //记录id
        String titles = "";
        if (introduction.getStatus() == 0) {
            //查出所有的状态为启用然后设为停用
            for (Introduction introduct : introductionService.selectList(new Introduction())) {
                if (introduct.getStatus() == 1) {
                    titles = titles.concat(introduct.getTitle());
                    introduct.setStatus(0);
                    introductionService.updateStatus(introduct);
                }
            }
        }


        Introduction intro = new Introduction();
        intro.setId(introduction.getId());
        intro.setStatus(introduction.getStatus() == 1 ? 0 : 1);
        introductionService.updateById(intro);


        if (!titles.isEmpty()) {
            return AjaxResult.success("已将" + titles + "设置为停用," + introduction.getTitle() + "已设置为启用!");
        } else {
            return AjaxResult.success(introduction.getTitle() + "已" + (introduction.getStatus().equals(1) ? "停用" : "启用"));
        }

    }
}
