package com.qianhe.zqbf.controller;

import com.qianhe.common.core.controller.BaseController;
import com.qianhe.common.core.domain.AjaxResult;
import com.qianhe.common.core.domain.model.LoginUser;
import com.qianhe.common.core.page.TableDataInfo;
import com.qianhe.common.exception.BusinessException;
import com.qianhe.common.utils.poi.ExcelUtil;
import com.qianhe.zqbf.domain.*;
import com.qianhe.zqbf.service.ZqbfXmzBfxqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/zqbf/bfxq")
public class ZqbfXmzBfxqController extends BaseController {
    @Autowired
    private ZqbfXmzBfxqService zqbfXmzBfxqService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqbfXmzBfxqQuery zqbfXmzBfxqQuery) {
        startPage();
        List<ZqbfXmzBfxqVo> dataList = zqbfXmzBfxqService.list(zqbfXmzBfxqQuery);
        return getDataTable(dataList);
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqbfXmzBfxq zqbfXmzBfxq) {
        try {
            return toAjax(zqbfXmzBfxqService.add(zqbfXmzBfxq, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqbfXmzBfxq zqbfXmzBfxq) {
        try {
            return toAjax(zqbfXmzBfxqService.edit(zqbfXmzBfxq, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        try {
            return toAjax(zqbfXmzBfxqService.delete(ids));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqbfXmzBfxqQuery zqbfXmzBfxqQuery) {
        List<ZqbfXmzBfxqVo> dataList = zqbfXmzBfxqService.list(zqbfXmzBfxqQuery);
        ExcelUtil<ZqbfXmzBfxqVo> util = new ExcelUtil<>(ZqbfXmzBfxqVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 提报需求
     */
    @PutMapping("/sub")
    public AjaxResult sub(@RequestBody ZqbfXmzBfxq zqbfXmzBfxq) {
        try {
            return toAjax(zqbfXmzBfxqService.sub(zqbfXmzBfxq, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-提报需求失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 撤销需求
     */
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestBody ZqbfXmzBfxq zqbfXmzBfxq) {
        try {
            return toAjax(zqbfXmzBfxqService.revoke(zqbfXmzBfxq, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-撤销需求失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 调度专家
     */
    @PostMapping("/dispatch")
    public AjaxResult dispatch(@RequestBody List<ZqczXmzDdzj> zqczXmzDdzjList) {
        try {
            return toAjax(zqbfXmzBfxqService.dispatch(zqczXmzDdzjList, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-调度专家失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 领导审核
     */
    @PostMapping("/approval")
    public AjaxResult approval(@RequestBody ZqczXmzApproval zqczXmzApproval) {
        try {
            return toAjax(zqbfXmzBfxqService.approval(zqczXmzApproval, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-领导审批失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 专家反馈
     */
    @PostMapping("/feedback")
    public AjaxResult feedback(@RequestBody ZqczXmzZjpj zqczXmzZjpj) {
        try {
            return toAjax(zqbfXmzBfxqService.feedback(zqczXmzZjpj, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-专家反馈失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 帮扶反馈
     */
    @PostMapping("/support")
    public AjaxResult support(@RequestBody ZqczXmzBffk zqczXmzBffk) {
        try {
            return toAjax(zqbfXmzBfxqService.support(zqczXmzBffk, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强帮扶-帮扶反馈失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }
}
