package io.renren.modules.business.controller;

import io.renren.common.utils.R;
import io.renren.modules.business.entity.BusinessApprovalEntity;
import io.renren.modules.business.entity.BusinessWorkerEntity;
import io.renren.modules.business.service.BusinessApprovalService;
import io.renren.modules.business.service.BusinessWorkerService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 审批数据接收表
 *
 * @author jiaqiao
 * @email jiaqiao@163.com
 * @date 2022-08-10 18:46:32
 */
@RestController
@RequestMapping("business/businessapproval")
public class BusinessApprovalController {
    @Autowired
    private BusinessApprovalService businessApprovalService;

    @Autowired
    private BusinessWorkerService businessWorkerService;

    /**
     * 列表
     */
    /*@RequestMapping("/list")
    @RequiresPermissions("business:businessapproval:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessApprovalService.queryPage(params);

        return R.ok().put("page", page);
    }*/
    @RequestMapping("/list")
    @RequiresPermissions("business:businessapproval:list")
    public R list(@RequestParam Map<String, Object> params){
        List<BusinessApprovalEntity> page = businessApprovalService.queryNewPage(params);
        return R.ok().put("page", page);
    }

    //子list
    @RequestMapping("/sonList/{approvalFatherId}")
    @RequiresPermissions("business:businessapproval:list")
    public R sonList(@PathVariable("approvalFatherId") Integer approvalFatherId){
        List<BusinessApprovalEntity> page = businessApprovalService.querySonList(approvalFatherId);
        return R.ok().put("page", page);
    }

    //出勤list
    @RequestMapping("/cqList")
    @RequiresPermissions("business:businessapproval:list")
    public R cqList(@RequestParam Map<String, Object> params) {
        List<BusinessApprovalEntity> page = businessApprovalService.queryCQList(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表  所有未离职人员列表 不含有翻页
     */
    @RequestMapping("/tcList")
    @RequiresPermissions("business:businessapproval:list")
    public R tcList(@RequestParam Map<String, Object> params) {
        List<BusinessWorkerEntity> page = businessWorkerService.queryNewPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:businessapproval:info")
    public R info(@PathVariable("id") Integer id){
		BusinessApprovalEntity businessApproval = businessApprovalService.getById(id);

        return R.ok().put("businessApproval", businessApproval);
    }

    /**
     * 出勤记录 以及 工资账目进入的接口
     */
    @RequestMapping("/getCqOrGzInfo/{id}")
    @RequiresPermissions("business:businessapproval:info")
    public R getCqOrGzInfo(@PathVariable("id") Integer id){
        BusinessApprovalEntity businessApproval = businessApprovalService.getCqOrGzInfo(id);

        return R.ok().put("businessApproval", businessApproval);
    }


    /**
     * 根据立项id获取立项信息合同信息
     */
    @RequestMapping("/getProjectConInfo/{projectId}")
    @RequiresPermissions("business:businessapproval:info")
    public R getProjectConInfo(@PathVariable("projectId") Integer projectId){
        Map<String, Object> map = businessApprovalService.getProjectConInfo(projectId);

        return R.ok().put("map", map);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessapproval:save")
    public R save(@RequestBody BusinessApprovalEntity businessApproval){
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();//获取当前登录人信息
        businessApproval.setApprovalApply(user.getAuthenticName());
        businessApproval.setApprovalApplyDate(new Date());
        System.out.println(businessApproval);
		businessApprovalService.save(businessApproval);

        if(businessApproval.getApprovalType() == 4) {
            // 取消合同审批过程，以后需要审批，则需要将↓面代码注释
            Map<String, Object> params = new HashMap<>();
            params.put("id", businessApproval.getId());
            params.put("approvalData", businessApproval.getApprovalData());
            params.put("approvalOldData", businessApproval.getApprovalOldData());
            params.put("approvalExcel", businessApproval.getApprovalSonData());
            params.put("approvalType", 4);
            businessApprovalService.pass(params);
        }

        if(businessApproval.getApprovalType() == 2) {
            // 取消人员管理-工资与账目审批过程，以后需要审批，则需要将↓面代码注释
            Map<String, Object> params = new HashMap<>();
            params.put("id", businessApproval.getId());
            params.put("approvalData", businessApproval.getApprovalData());
            params.put("approvalOldData", businessApproval.getApprovalOldData());
            params.put("approvalExcel", businessApproval.getApprovalSonData());
            params.put("approvalType", 2);
            businessApprovalService.pass(params);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessapproval:update")
    public R update(@RequestBody BusinessApprovalEntity businessApproval){
		businessApprovalService.updateById(businessApproval);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businessapproval:delete")
    public R delete(@RequestBody Integer[] ids){
		businessApprovalService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 需要传入：新的实体的所有数据字段，对应审批的id,对应的approval_type类型字段
     */
    @RequestMapping("/pass")
    @RequiresPermissions("business:businessapproval:update")
    public R pass(@RequestParam Map<String, Object> params){
        businessApprovalService.pass(params);
        return R.ok();
    }

    /**
     * 需要传入：对应的业务idList 业务类型 对应的审批表的idList
     */
    @RequestMapping("/noPass")
    @RequiresPermissions("business:businessapproval:update")
    public R noPass(@RequestParam Map<String, Object> params){
        businessApprovalService.noPass(params);
        return R.ok();
    }


}