package com.qianhe.zqcz.xmrylsjl.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.xmrylsjl.domain.ZqczXmRyLsjl;
import com.qianhe.zqcz.xmrylsjl.domain.ZqczXmRyLsjlQuery;
import com.qianhe.zqcz.xmrylsjl.domain.ZqczXmRyLsjlVo;
import com.qianhe.zqcz.xmrylsjl.service.ZqczXmRyLsjlService;
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/lsjl")
public class ZqczXmRyLsjlController extends BaseController {
    @Autowired
    private ZqczXmRyLsjlService zqczXmRyLsjlService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public AjaxResult list(ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        return success(zqczXmRyLsjlService.list(zqczXmRyLsjlQuery));
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqczXmRyLsjl zqczXmRyLsjl) {
        try {
            return toAjax(zqczXmRyLsjlService.edit(zqczXmRyLsjl, getUsername()));
        } 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 ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        try {
            return toAjax(zqczXmRyLsjlService.spotCheckAdd(zqczXmRyLsjlQuery));
        } catch (BusinessException e) {
            log.warn("最强操作-项目抽检失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        List<ZqczXmRyLsjlVo> dataList = zqczXmRyLsjlService.list(zqczXmRyLsjlQuery);
        ExcelUtil<ZqczXmRyLsjlVo> util = new ExcelUtil<>(ZqczXmRyLsjlVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 现场抽取情况统计
     */
    @GetMapping("/spotCheckStatus")
    public AjaxResult spotCheckStatus(ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        try {
            return success(zqczXmRyLsjlService.spotCheckStatus(zqczXmRyLsjlQuery));
        } catch (BusinessException e) {
            log.warn("最强操作-项目人员修改失败" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 现场抽取情况表导出
     */
    @PostMapping("/spotCheckStatus/export")
    public void spotCheckStatusExport(HttpServletResponse response, @RequestBody ZqczXmRyLsjlQuery zqczXmRyLsjlQuery) {
        zqczXmRyLsjlService.spotCheckStatusExport(response, zqczXmRyLsjlQuery);
    }

    /**
     * 修改盲抽人员名称
     */
    @PutMapping("/editRymc")
    public AjaxResult editRymc(@RequestBody ZqczXmRyLsjl zqczXmRyLsjl) {
        try {
            return toAjax(zqczXmRyLsjlService.editRymc(zqczXmRyLsjl));
        } catch (BusinessException e) {
            log.warn("最强操作-盲抽人员名称修改失败" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }
}
