package com.tofly.feesapi.yhgl.controller;

import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;

import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.yhgl.entity.YhGroupAccounts;
import com.tofly.feesapi.yhgl.entity.dto.YhGroupAccountsFilter;
import com.tofly.feesapi.yhgl.entity.vo.YhGroupAccountsSelect;
import com.tofly.feesapi.yhgl.service.YhGroupAccountsService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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


@RestController
@RequestMapping("/yhgroupaccounts")
@Api(tags = "用户分组用户账户接口")
public class YhGroupAccountsController {

    @Autowired
    private YhGroupAccountsService yhGroupAccountsService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询", tags = "档案管理")
    public ResultRespone<PageResult<YhGroupAccountsSelect>> getPage(@ModelAttribute PageDto pageDto, @ModelAttribute YhGroupAccountsFilter filter) {
        return ResultRespone.success(yhGroupAccountsService.getPage(pageDto, filter));
    }

    @ApiOperation(value = "新增用户分组用户账户", tags = "档案管理")
    @PostMapping
    @Transactional
    public ResultRespone save(@RequestBody List<YhGroupAccounts> yhGroupAccountsList) {
        if (yhGroupAccountsList.isEmpty()) {
            throw new FeesException("用户分组用户账户不能为空！", FeesErrorCode.BUSINESS_ERROR);
        }
        for (YhGroupAccounts yhGroupAccount : yhGroupAccountsList) {
            if (StringUtil.isEmpty(yhGroupAccount.getYhbh())) {
                throw new FeesException("用户编号不能为空！", FeesErrorCode.BUSINESS_ERROR);
            }
            if (StringUtil.isEmpty(yhGroupAccount.getGroupId())) {
                throw new FeesException("用户群组编号不能为空！", FeesErrorCode.BUSINESS_ERROR);
            }
            yhGroupAccount.setCzry(SecurityUtils.getUserId().toString());
            yhGroupAccount.setCzrymc(SecurityUtils.getUser().getRealName());

            yhGroupAccount.setCzsj(new Date());
            yhGroupAccount.setId(yhGroupAccountsService.getMaxid());
            yhGroupAccountsService.save(yhGroupAccount);
        }
        return ResultRespone.success(true);
    }

    @ApiOperation(value = "通过ID删除用户分组用户账户", tags = "档案管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(yhGroupAccountsService.removeById(id));
    }

    @ApiOperation(value = "通过ID批量删除用户分组用户账户", tags = "档案管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(yhGroupAccountsService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
