package com.qianhe.zqcz.jcrw.controller;

import com.qianhe.common.core.controller.BaseController;
import com.qianhe.common.core.domain.AjaxResult;
import com.qianhe.common.core.page.TableDataInfo;
import com.qianhe.common.exception.BusinessException;
import com.qianhe.common.utils.poi.ExcelUtil;
import com.qianhe.zqcz.jcrw.domain.ZqczJccjQuery;
import com.qianhe.zqcz.jcrw.domain.ZqczJcrw;
import com.qianhe.zqcz.jcrw.domain.ZqczJcrwQuery;
import com.qianhe.zqcz.jcrw.domain.ZqczJcrwVo;
import com.qianhe.zqcz.jcrw.mapper.ZqczJcrwMapper;
import com.qianhe.zqcz.jcrw.service.ZqczJcrwService;
import com.qianhe.zqcz.zsrw.service.ZqczZsrwService;
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;

@Slf4j
@RestController
@RequestMapping("/zqcz/zqczjcrw")
public class ZqczJcrwController extends BaseController {
    @Autowired
    private ZqczJcrwService zqczJcrwService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczJcrwQuery zqczJcrwQuery) {
        startPage();
        return getDataTable(zqczJcrwService.list(zqczJcrwQuery));
    }

    /**
     * 查询详情
     */
    @GetMapping("/{id}")
    public AjaxResult info(@PathVariable Long id) {
        try {
            return success(zqczJcrwService.info(id));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

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

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

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqczJcrwQuery zqczJcrwQuery) {
        List<ZqczJcrwVo> dataList = zqczJcrwService.list(zqczJcrwQuery);
        ExcelUtil<ZqczJcrwVo> util = new ExcelUtil<>(ZqczJcrwVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 根据直属任务ID 删除
     */
    @DeleteMapping("/deleteByZsrwid/{zsrwid}")
    public AjaxResult deleteByZsrwid(@PathVariable Long zsrwid) {
        try {
            return toAjax(zqczJcrwService.deleteByZsrwid(zsrwid));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 基层抽检查询
     */
    @GetMapping("/jcrwInfo")
    public AjaxResult jcrwInfo(ZqczJcrwQuery zqczJcrwQuery) {
        try {
            return success(zqczJcrwService.jcrwInfo(zqczJcrwQuery));
        } catch (BusinessException e) {
            log.warn("最强操作-基层任务-基层抽检查询" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 基层任务抽取
     */
    @PostMapping("/spotCheckAdd")
    public AjaxResult spotCheckAdd(@RequestBody ZqczJccjQuery zqczJccjQuery) {
        try {
            return toAjax(zqczJcrwService.spotCheckAdd(zqczJccjQuery));
        } catch (BusinessException e) {
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }
}
