package com.zjrs.ggfw.rlzysc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.bpo.GxygBPO;
import com.zjrs.ggfw.rlzysc.dto.GxygDTO;
import com.zjrs.ggfw.rlzysc.dto.GxygQueryDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

@Api("共享用工")
@RestController
@RequestMapping("/api/rlzysc/gxyg")
public class GxygController {

    private static final Logger LOG = LoggerFactory.getLogger(GxygController.class);

    @Resource
    private GxygBPO gxygBPO;

    /**
     * 获取岗位信息列表
     *
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "获取共享用工信息列表", notes = "获取共享用工信息列表")
    @PostMapping("/list")
    public AjaxResponse list(@RequestBody @Valid GxygQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        Integer pagenum = Math.toIntExact(dto.getPagenum());
        Integer pagesize = Math.toIntExact(dto.getPagesize());
        try {
            PageResult pageResult = gxygBPO.queryGxygList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 获取岗位信息列表
     *
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "获取共享用工信息", notes = "获取共享用工信息")
    @PostMapping("/details")
    public AjaxResponse details(@RequestBody @Valid GxygDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            ajaxResponse.setData("data", gxygBPO.queryGxyg(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "保存共享用工信息", notes = "保存共享用工信息")
    @PostMapping("/save")
    public AjaxResponse saveOrUpdate(@RequestBody @Valid GxygDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            ajaxResponse.setData("ywlsh", this.gxygBPO.saveOrUpdateGxyg(dto));
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");
        } catch (AppException e) {
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "删除共享用工信息", notes = "删除共享用工信息")
    @PostMapping("/delete")
    public AjaxResponse delete(@RequestBody @Valid GxygDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            this.gxygBPO.delete(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");

        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "提交共享用工信息", notes = "提交共享用工信息")
    @PostMapping("/submit")
    public AjaxResponse submit(@RequestBody @Valid GxygDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            this.gxygBPO.submit(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");

        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "发布/下架共享用工信息", notes = "发布/下架共享用工信息")
    @PostMapping("/release")
    public AjaxResponse release(@RequestBody @Valid GxygDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            this.gxygBPO.release(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");

        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取共享用工功能启用关闭状态", notes = "获取共享用工功能启用关闭状态")
    @PostMapping("/status")
    public AjaxResponse status() {
        AjaxResponse ajaxResponse = new AjaxResponse();

        try {

            ajaxResponse.setData("data", this.gxygBPO.status());
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");

        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
}

