package com.goldenwater.web.controller.pms;

import com.goldenwater.slgc.entity.pms.PmsMeeting;
import com.goldenwater.slgc.entity.pms.PmsProject;
import com.goldenwater.slgc.service.pms.IPmsMeetingService;
import com.goldenwater.slgc.service.pms.IPmsAccessoryService;
import com.goldenwater.slgc.service.pms.IPmsProjectService;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@RestController
@Validated
@RequestMapping("/pms/meeting")
public class PmsMeetingController extends BaseController {
    @Autowired
    private IPmsMeetingService pmsMeetingService;

    @Autowired
    private IPmsAccessoryService pmsAccessoryService;

    @Autowired
    private IPmsProjectService pmsProjectService;

    /**
     * 查询会议纪要列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PmsMeeting meeting) {
        startPage();
        List<PmsMeeting> list = pmsMeetingService.selectList(meeting);
        return getDataTable(list);
    }

    /**
     * 获取会议纪要详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(pmsMeetingService.selectById(id));
    }

    /**
     * 新增会议纪要
     */
    @Log(title = "会议管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsMeeting meeting) {
        if (meeting.getPrjId() != null) {
            PmsProject project = pmsProjectService.selectById(meeting.getPrjId());
            if (project != null) {
                meeting.setPrjName(project.getPrjName());
            }
        }
        int result = pmsMeetingService.insert(meeting);
        if (result > 0 && StringUtils.isNotBlank(meeting.getAccessory())) {
            String[] ids = meeting.getAccessory().split(";");
            for (String id : ids) {
                if (StringUtils.isNotBlank(id)) {
                    pmsAccessoryService.updateMtId(Long.valueOf(id.trim()), meeting.getMtId());
                }
            }
        }
        return toAjax(result);
    }

    /**
     * 修改会议纪要
     */
    @Log(title = "会议管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsMeeting meeting) {
        int result = pmsMeetingService.update(meeting);
        if (result > 0 && StringUtils.isNotBlank(meeting.getAccessory())) {
            String[] ids = meeting.getAccessory().split(";");
            for (String id : ids) {
                if (StringUtils.isNotBlank(id)) {
                    pmsAccessoryService.updateMtId(Long.valueOf(id.trim()), meeting.getMtId());
                }
            }
        }
        return toAjax(result);
    }

    /**
     * 删除会议纪要
     */
    @Log(title = "会议管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsMeetingService.deleteByIds(ids));
    }
}
