package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckGroup;
import com.nantian.cloud.jly.service.CheckGroupService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.web.bind.annotation.*;

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


/**
 * 审查组成员
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "companyCalculater", tags = "审查组成员")
@RequestMapping("/checkGroup")
public class CheckGroupController {
    private final CheckGroupService checkGroupService;


    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param checkGroup 请假审批单
     * @return
     */
    @SysLog("查询审查组成员")
    @ApiOperation(value = "查询审查组成员", notes = "条件查询审查组成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CompanyCalculater", value = "审查组成员", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/page")
    public R getcheckGroupPage(Page page, CheckGroup checkGroup) {
        checkGroup.setDelFlag("1"); //查询未删除数据
        return R.ok(checkGroupService.page(page, Wrappers.query(checkGroup)));
    }


    /**
     * 信息
     *
     * @param checkGroupId
     * @return R
     */
    @SysLog("通过id查询审查组成员")
    @ApiOperation(value = "通过id查询审查组成员", notes = "通过id条件查询审查组成员")
    @ApiImplicitParam(name = "checkGroupId", value = "审查组成员", required = true, paramType = "path", dataType = "int")
    @GetMapping("/{checkGroupId}")
    public R getById(@PathVariable("checkGroupId") Integer checkGroupId) {
        return R.ok(checkGroupService.getById(checkGroupId));
    }

    /**
     * 保存
     *
     * @param checkGroup
     * @return R
     */
    @SysLog("保存审查组成员")
    @ApiOperation(value = "保存审查组成员", notes = "新增审查组成员")
    @ApiImplicitParam(name = "CompanyCalculater", value = "审查组成员", required = true, dataType = "json", paramType = "path")
    @PostMapping
    public R save(@RequestBody CheckGroup checkGroup) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkGroup.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkGroup.setDeptId(u.getDeptId());
        }
        checkGroup.setDelFlag("1");
        checkGroup.setUpdateTime(new Date());
        return R.ok(checkGroupService.save(checkGroup));
    }

    /**
     * 修改
     *
     * @param checkGroup
     * @return R
     */
    @SysLog("修改审查组成员信息")
    @ApiOperation(value = "修改审查组成员信息", notes = "修改审查组成员信息")
    @ApiImplicitParam(name = "CompanyCalculater", value = "审查组成员", required = true, dataType = "json", paramType = "path")
    @PutMapping
    public R updateById(@RequestBody CheckGroup checkGroup) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkGroup.setUserid(u.getId());
        }
        checkGroup.setUpdateTime(new Date());
        return R.ok(checkGroupService.updateById(checkGroup));
    }

    /**
     * 批量保存/修改
     *
     * @param checkGroupList
     * @return R
     */
    @SysLog("审查组成员批量保存/修改")
    @PostMapping("/add")
    public R add(@RequestBody List<CheckGroup> checkGroupList) {
        ScUser u = SecurityUtils.getUser();
        checkGroupList.forEach(r->{
            if (u.getId() != null) {
                r.setUserid(u.getId());
            }
            if (u.getDeptId() != null) {
                r.setDeptId(u.getDeptId());
            }
            r.setDelFlag("1");
            r.setUpdateTime(new Date());
        });
        return R.ok(checkGroupService.saveOrUpdateBatch(checkGroupList));
    }

    /**
     * 删除
     *
     * @param checkPlanId
     * @return R
     */
    @SysLog("删除审查组成员")
    @ApiOperation(value = "删除审查组成员", notes = "删除审查组成员")
    @ApiImplicitParam(name = "checkPlanId", value = "审查组成员", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("del")
    public R removeByCheckPlanId(Long checkPlanId) {
        checkGroupService.deleteBycheckPlanId(checkPlanId);
        return R.ok();
    }

    /**
     * 根据id删除
     *
     * @param delId
     * @return R
     */
    @SysLog("审查组成员根据id删除")
    @DeleteMapping("/delById/{delId}")
    public R delById(@PathVariable Long delId) {
        return R.ok(checkGroupService.removeById(delId));
    }

    /**
     * 删除
     *
     * @param checkPlanId
     * @return R
     */
    @SysLog("审查组成员-根据id删除")
    @ApiOperation(value = "删除审查组成员", notes = "删除审查组成员")
    @ApiImplicitParam(name = "checkAbnormalReportId", value = "审查组成员", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{checkPlanId}")
    public R removeById(@PathVariable("checkPlanId") Long checkPlanId) {
        checkGroupService.deleteBycheckPlanId(checkPlanId);
//        CheckGroup checkGroup = checkGroupService.getById(checkGroupId);
//        ScUser u = SecurityUtils.getUser();
//        if (u.getId() != null) {
//            checkGroup.setUserid(u.getId());
//        }
//        checkGroup.setDelFlag("4");
//        checkGroup.setUpdateTime(new Date());
//        return R.ok(checkGroupService.updateById(checkGroup));
        return R.ok();
    }

}
