package cn.timer.api.controller.zpgl;

import cn.timer.api.bean.zpgl.*;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.config.exception.CustomException;
import cn.timer.api.controller.kqgl.atttimer.RealTimeUpdate;
import cn.timer.api.controller.zpgl.sevice.ZpglService;
import cn.timer.api.dao.kqgl.UserEquiRelationMapper;
import cn.timer.api.dao.kqmk.KqglAssoKqzdkfsMapper;
import cn.timer.api.dao.qyzx.QyzxEmpLoginMapper;
import cn.timer.api.dao.yggl.YgglMainEmpMapper;
import cn.timer.api.dao.zpgl.ZpglRcxxMapper;
import cn.timer.api.dto.zpgl.ZpglZwxxAddUpdateDto;
import cn.timer.api.dto.zpgl.ZpglZwxxDto;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import cn.timer.api.utils.query.BaseQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 招聘管理-招聘职位
 *
 * @author wuqingjun
 * @email 284718418@qq.com
 * @date 2022-03-22 15:14:40
 */
@Api(tags = "招聘管理")
@Transactional(rollbackOn = Exception.class)
@RestController
@RequestMapping(value = "/zpzw", produces = {"application/json"})
@Slf4j
public class ZpglZwxxController {

    /**
     * 招聘职位列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "1.招聘职位列表", httpMethod = "GET", notes = "招聘职位表")
    @ApiOperationSupport(order = 1)
    public Result<Object> recruit(@CurrentUser UserBean userBean, BaseQuery baseQuery,
                                  @ApiParam("搜索关键字") @RequestParam(required = false) String query,
                                  @ApiParam("职位ID") @RequestParam(required = false) Integer zpglZwxxId,
                                  @ApiParam("状态") @RequestParam(required = false) Integer status) {
        try {
            Page<ZpglZwxx> page = new Page<ZpglZwxx>(baseQuery.getPageNum(), baseQuery.getPageSize());
            MPJQueryWrapper<ZpglZwxx> queryWrapper = new MPJQueryWrapper<>();
            queryWrapper.selectAll(ZpglZwxx.class).select("zg.gzdd_name","zg.province","zg.district","zg.city")
                    .leftJoin("zpgl_gzdd zg on t.zpgl_gzdd_id = zg.id");
            queryWrapper.eq("t.org_code", userBean.getOrgCode()).eq("t.delete_flag",0);
            if (!StringUtils.isEmpty(zpglZwxxId) && zpglZwxxId > 0) {
                queryWrapper.eq("t.id", zpglZwxxId);
            }
            if (!StringUtils.isEmpty(query)) {
                queryWrapper.like("t.name", query);
            }
            queryWrapper.eq("t.status", status).orderByDesc("t.create_time");
            IPage<ZpglZwxx> zpglZwxxPage = ZpglZwxx.builder().build().selectPage(page, queryWrapper);
            List<ZpglZwxx> zpglRcxxs = page.getRecords();
            zpglZwxxPage.getCurrent();
            zpglZwxxPage.getPages();
            zpglZwxxPage.getTotal();
            zpglZwxxPage.getSize();
            return ResultUtil.data(zpglZwxxPage, zpglRcxxs, "搜索成功");

        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("获取招聘职位列表失败");
        }

    }

    /**
     * 招聘职位-停止招聘
     *
     * @param
     * @return
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "2.招聘职位-停止招聘", httpMethod = "POST", notes = "招聘职位-停止招聘")
    @ApiOperationSupport(order = 2)
    public Result<Object> edit(@CurrentUser UserBean userBean, @RequestBody ZpglZwxxDto zpglZwxxDto) {
        try {
            return ZpglZwxx.builder().id(zpglZwxxDto.getId()).status(zpglZwxxDto.getStatus()).userId(userBean.getEmpNum()).userName(userBean.getQyzxEmpLogin().getUsername()).build().updateById()?ResultUtil.success("操作成功"):ResultUtil.error("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("招聘-删除人才信息失败");
        }

    }
    /**
     * 招聘职位-删除
     *
     * @param
     * @return
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "3.招聘职位-删除", httpMethod = "POST", notes = "招聘职位-删除")
    @ApiOperationSupport(order = 3)
    public Result<Object> delete(@CurrentUser UserBean userBean, @RequestBody ZpglZwxxDto zpglZwxxDto) {
        try {
            return ZpglZwxx.builder().id(zpglZwxxDto.getId()).deleteFlag(zpglZwxxDto.getDeleteFlag()).userId(userBean.getEmpNum()).userName(userBean.getQyzxEmpLogin().getUsername()).build().updateById()?ResultUtil.success("操作成功"):ResultUtil.error("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("招聘-删除人才信息失败");
        }

    }
    /**
     * 招聘职位-添加
     *
     * @param
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "4.招聘职位-添加", httpMethod = "POST", notes = "招聘职位-添加")
    @ApiOperationSupport(order = 4)
    public Result<Object> add(@CurrentUser UserBean userBean, @RequestBody ZpglZwxxAddUpdateDto zpglZwxxAddUpdateDto) {
        try {
            ZpglZwxx zpglZwxx = new ZpglZwxx();
            BeanUtils.copyProperties(zpglZwxxAddUpdateDto,zpglZwxx);
            zpglZwxx.setUserId(userBean.getEmpNum());
            zpglZwxx.setUserName(userBean.getQyzxEmpLogin().getUsername());
            zpglZwxx.setStatus(1);
            zpglZwxx.setOrgCode(userBean.getOrgCode().toString());
            return zpglZwxx.insertOrUpdate()?ResultUtil.success("操作成功"):ResultUtil.error("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("招招聘职位-添加失败");
        }

    }
}