package com.skr.mdm.api.app.controller;

import com.skr.mdm.api.app.service.AppConfigService;
import com.skr.mdm.bean.qo.BaseQueryQO;
import com.skr.mdm.bean.qo.app.AppConfigQO;
import com.skr.mdm.bean.vo.appstore.AppConfigVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

@Validated
@RestController
@RequestMapping("appConfig")
@Api(tags = "应用配置")
public class AppConfigController {
	@Autowired
	private AppConfigService appConfigService;

	@PostMapping("list")
	@ApiOperation("列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "ok", response = AppConfigVO.class)
	})
	@PreAuthorize("hasAnyAuthority(new String[] {'sys:app:strategy:config:list', 'sys:app:strategy:add', " +
			"'sys:app:strategy:update', 'sys:app:strategy:uninstall:add'})")
	public ResponseResult list(@RequestBody @Valid BaseQueryQO baseQueryQO, @ApiIgnore Authentication authentication) {
		LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
		return appConfigService.list(baseQueryQO, loginAdminDTO);
	}

	@PostMapping("add")
	@ApiOperation("新增")
	@PreAuthorize("hasAnyAuthority('sys:app:strategy:config:add')")
	public ResponseResult add(@RequestBody @Valid List<AppConfigQO> appConfigQOList, @ApiIgnore Authentication authentication) {
		LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
		return appConfigService.add(appConfigQOList, loginAdminDTO);
	}

	@PostMapping("update")
	@ApiOperation("编辑")
	@PreAuthorize("hasAnyAuthority('sys:app:strategy:config:update')")
	public ResponseResult update(@RequestBody @Valid AppConfigQO appConfigQO, @ApiIgnore Authentication authentication) {
		LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
		return appConfigService.update(appConfigQO, loginAdminDTO);
	}


	@ApiOperation(value = "删除")
	@PostMapping("/delete")
	@PreAuthorize("hasAnyAuthority('sys:app:strategy:config:delete')")
	public ResponseResult delete(@RequestParam @NotEmpty(message = "id不能为空") List<Integer> idList,
								 @ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return appConfigService.delete(idList, admin);
	}

	@ApiOperation(value = "导入")
	@PostMapping("import")
	@PreAuthorize("hasAnyAuthority('sys:app:strategy:config:export')")
	public ResponseResult importFile(MultipartFile file, @ApiIgnore Authentication authentication) throws IOException {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return appConfigService.importFile(file, admin);
	}

}