package cn.ezhizao.project.business.consultResult.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.consult.domain.BizDayworkItemConsult;
import cn.ezhizao.project.business.consult.service.IBizDayworkItemConsultService;
import cn.ezhizao.project.business.consultResult.domain.BizConsultResult;
import cn.ezhizao.project.business.consultResult.service.IBizConsultResultService;
import org.springframework.security.access.prepost.PreAuthorize;
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.DeleteMapping;
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;


/**
 * 咨询结果维护Controller
 *
 * @author ezhizao
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/business/consultResult")
public class BizConsultResultController extends BaseController
{
    @Resource
    private IBizConsultResultService bizConsultResultService;
    @Resource
    private IBizDayworkItemConsultService bizDayworkItemConsultService;

    /**
     * 查询咨询结果维护列表
     */
    @Log(title = "咨询结果维护查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizConsultResult bizConsultResult) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizConsultResult);
        startPage();
        List<BizConsultResult> list = bizConsultResultService.getList(bizConsultResult);
        return getDataTable(list);
    }

    /**
     * 导出咨询结果维护列表
     */
    @PreAuthorize("@ss.hasPermi('business:consultResult:export')")
    @Log(title = "咨询结果维护导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizConsultResult bizConsultResult) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizConsultResult);
        List<BizConsultResult> list = bizConsultResultService.getList(bizConsultResult);
        ExcelUtil<BizConsultResult> util = new ExcelUtil<BizConsultResult>(BizConsultResult.class);
        util.exportExcel(response, list, "咨询结果维护数据");
    }

    /**
     * 获取咨询结果维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:consultResult:query')")
    @Log(title = "咨询结果维护查询详细信息", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizConsultResultService.getById(id));
    }

    /**
     * 新增咨询结果维护
     */

    @Log(title = "咨询结果维护新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizConsultResult bizConsultResult) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizConsultResult);
        return toAjax(bizConsultResultService.save(bizConsultResult));
    }

    /**
     * 修改咨询结果维护
     */
    @Log(title = "咨询结果维护修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizConsultResult bizConsultResult) throws NoSuchFieldException, IllegalAccessException
    {

        //判断如果咨询使用该咨询结果，则不允许删除
        List<BizDayworkItemConsult> list = bizDayworkItemConsultService.query().eq("consult_result_id", bizConsultResult.getId()).list();
        if(!list.isEmpty()){
            if(!list.get(0).getStatus().equals(bizConsultResult.getStatus())){
                return error("当前咨询结果已被使用，不能修改放行状态");
            }

        }
        return toAjax(bizConsultResultService.updateById(bizConsultResult));
    }

    /**
     * 删除咨询结果维护
     */
    @Log(title = "咨询结果维护删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        //判断如果咨询使用该咨询结果，则不允许删除
        List<BizDayworkItemConsult> list = bizDayworkItemConsultService.query().in("consult_result_id", ids).list();
        if(!list.isEmpty()){
            return error("当前咨询结果已被使用，不能删除");
        }

        return toAjax(bizConsultResultService.removeBatchByIds(ids));
    }
}
