package cn.timer.api.controller.zpgl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.timer.api.aspect.lang.annotation.Log;
import cn.timer.api.aspect.lang.enums.BusinessType;
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.bean.zpgl.ZpglMslcLog;
import cn.timer.api.bean.zpgl.ZpglRcxx;
import cn.timer.api.bean.zpgl.ZpglRcxxMaterial;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.dao.zpgl.ZpglRcxxMaterialMapper;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;


/**
 * 入职申请材料附件
 *
 * @author wgd
 * @email 862422848@qq.com
 * @date 2022-05-12 18:00:06
 */
@Api(tags = "招聘管理")
@Transactional(rollbackOn = Exception.class)
@RestController
@RequestMapping("/zpgl/rzcl")
public class ZpglRcxxMaterialController {
    @Autowired
    private ZpglRcxxMaterialMapper zpglRcxxMaterialMapper;

    @PostMapping("/subrz")
    @ApiOperation(value = "提交入职材料", httpMethod = "GET", notes = "查询面试申请信息")
    @Log(title = "interviewApplicationIofo", businessType = BusinessType.INSERT_UPDATE)
    public Result<Object> subrz(@CurrentUser UserBean userBean,
                                @RequestBody ZpglRcxxMaterial zpglRcxxMaterial) {

        if (zpglRcxxMaterial.getZpglRcxxId() <= 0 || zpglRcxxMaterial.getZpglRcxxId() == null) {
            return ResultUtil.error("人才不存在");
        }
        if (zpglRcxxMaterial.getDataList().size() <= 0) {
            return ResultUtil.error("文件不存在");
        }
        ZpglRcxx zpglRcxx = ZpglRcxx.builder().id(zpglRcxxMaterial.getZpglRcxxId()).build().selectById();
        if (zpglRcxx == null) {
            return ResultUtil.error("人才不存在");
        }
        zpglRcxxMaterial.getDataList().stream().forEach(v->{
            ZpglRcxxMaterial data  = new ZpglRcxxMaterial();
            data.setOrgCode(userBean.getOrgCode());
            data.setCreateTime(new Date());
            data.setZpglRcxxId(zpglRcxxMaterial.getZpglRcxxId());
            data.setType(Integer.parseInt(v.get("type")));
            data.setUrl(v.get("url").toString());
            data.insert();
        });
        return ResultUtil.data("提交成功");
    }

    @GetMapping("/materialList")
    @ApiOperation(value = "入职材料列表", httpMethod = "GET", notes = "查询面试申请信息")
    @Log(title = "interviewApplicationIofo", businessType = BusinessType.INSERT_UPDATE)
    public Result<Object> materialList(@CurrentUser UserBean userBean,Integer empNum) {

        YgglMainEmp ygglMainEmp = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>()
                .lambda().eq(YgglMainEmp::getOrgCode,userBean.getOrgCode())
        .eq(YgglMainEmp::getEmpNum,empNum));
        if(ygglMainEmp==null){
            return ResultUtil.error("用户不存在");
        }
        List<ZpglRcxxMaterial> list = ZpglRcxxMaterial.builder().build().selectList(new QueryWrapper<ZpglRcxxMaterial>().lambda()
        .eq(ZpglRcxxMaterial::getNumId,empNum));
        if(list.size()<=0){
            return ResultUtil.error("当前用户没有材料");
        }
        return ResultUtil.data(list);
    }

}