package com.nantian.cloud.jly.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckGroupPersons;
import com.nantian.cloud.jly.service.CheckGroupPersonsService;

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.ApiOperation;
import lombok.AllArgsConstructor;
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("/checkGroupPersons")
public class CheckGroupPersonsController {

    private final CheckGroupPersonsService checkGroupPersonsService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @SysLog("审查组成员-查询")
    @GetMapping("/page")
    public R getcheckGroupPage(Page page) {
        return R.ok(checkGroupPersonsService.getcheckGroupPersonsPage(page));
    }


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

    @SysLog("审查组成员-更新")
    @PostMapping("/update")
    public R update(@RequestBody List<CheckGroupPersons> checkGroupPersons) {
        ScUser u = SecurityUtils.getUser();
        for (CheckGroupPersons persons: checkGroupPersons) {
            if (u.getId() != null) {
                persons.setUserid(u.getId());
            }
            if (u.getDeptId() != null) {
                persons.setDeptId(u.getDeptId());
            }
            persons.setDelFlag("1");
            persons.setUpdateTime(new Date());
        }
        return R.ok(checkGroupPersonsService.updateBatchById(checkGroupPersons));
    }

    /**
     * 删除
     *
     * @param name
     * @return R
     */
    @SysLog("审查组成员-删除")
    @ApiOperation(value = "删除审查组成员", notes = "删除审查组成员")
    @ApiImplicitParam(name = "CheckGroupPersons", value = "审查组成员", required = true, dataType = "json", paramType = "path")
    @DeleteMapping("/{name}")
    public R removeById(@PathVariable("name") String name) {
        checkGroupPersonsService.deleteByName(name);
        return R.ok();
    }

}
