package com.ozs.kpim.controller.kpiconfig;

import com.ozs.kpim.controller.common.IBaseController;
import com.ozs.kpim.dto.kpiconfig.KpiConfigDTO;
import com.ozs.kpim.dto.kpiconfig.KpiConfigForListDTO;
import com.ozs.kpim.service.kpiconfig.AccountApi;
import com.ozs.kpim.service.kpiconfig.KpiConfigService;
import com.ozs.kpim.util.R;
import com.ozs.kpim.util.S;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ConfigManagerController
 * @Desc KPI配置管理接口
 * @Author jade
 * @Date 2019/2/19
 * @Version 1.0
 */
@RestController
@Api(tags = { "KPI配置管理接口" })
public class KpiConfigManagerController implements IBaseController<KpiConfigDTO> {

	@Resource
	private KpiConfigService kpiConfigService;

	@Resource
	private AccountApi accountApi;

	@ApiOperation("创建KPI一条")
	@PostMapping("/kpim/kpiConfig/save")
	@Override
	public R<?> save(@RequestBody KpiConfigDTO kpiConfigDTO, @RequestHeader("token") String token) {
		try {
			kpiConfigDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return kpiConfigService.save(kpiConfigDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	@ApiOperation("修改KPI配置")
	@PutMapping("/kpim/kpiConfig/update")
	@Override
	public R<?> update(@RequestBody KpiConfigDTO kpiConfigDTO, @RequestHeader("token") String token) {
		try {
			kpiConfigDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return kpiConfigService.update(kpiConfigDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	@ApiOperation("启用/停用")
	@PutMapping("/kpim/kpiConfig/updateEnableStatus")
	public R<?> updateEnableStatus(@RequestBody KpiConfigDTO kpiConfigDTO) {
		try {
			return kpiConfigService.updateEnableStatus(kpiConfigDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	@ApiOperation("删除KPI配置")
	@DeleteMapping("/kpim/kpiConfig/delete/{id}")
	@Override
	public R<?> delete(@PathVariable String id) {
		try {
			return kpiConfigService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	@ApiOperation("查看KPI配置详情")
	@GetMapping("/kpim/kpiConfig/getById/{id}")
	@Override
	public R<KpiConfigDTO> getById(@PathVariable String id) {
		try {
			return kpiConfigService.getById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	@ApiOperation("查询KPI配置列表（分页）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "主题名称", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "type", value = "KPI类型ID", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "enableStatus", value = "启用状态", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "editStatus", value = "编辑状态", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "topicIds", value = "所属主题Id数组", required = false, paramType = "query", allowMultiple = true, dataType = "String"),
			@ApiImplicitParam(name = "topicCodes", value = "所属主题编码数组", required = false, paramType = "query", allowMultiple = true, dataType = "String"),
			@ApiImplicitParam(name = "sort", value = "排序字段", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "order", value = "排序", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/kpim/kpiConfig/listKpiConfigs")
	public R<List<KpiConfigForListDTO>> listKpiConfigs(@RequestParam(required = false) String name,
			@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer enableStatus,
			@RequestParam(required = false) Integer editStatus, @RequestParam(required = false) String[] topicIds,
			@RequestParam(required = false) String[] topicCodes,
			@RequestParam(required = false, defaultValue = "update_time") String sort,
			@RequestParam(required = false, defaultValue = "desc") String order,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize) throws Exception {

		try {
			Map<String, Object> params = new HashMap<>();
			params.put("name", name);
			params.put("type", type);
			params.put("enableStatus", enableStatus);
			params.put("editStatus", editStatus);
			params.put("topicIds", topicIds);
			params.put("topicCodes", topicCodes);
			return kpiConfigService.listKpiConfigs(params, sort, order, page, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

	/*
	 * *
	 * 
	 * @Author jade
	 * 
	 * @Description 批量创建 （定义/输入）类型KPI方法
	 * 
	 * @Date 2019/3/29
	 * 
	 * @Param [kpiList]
	 * 
	 * @return R<?>
	 **/
	@ApiOperation("创建KPI批量新增")
	@PostMapping("/kpim/kpiConfig/batchSave")
	public R<?> batchSave(@RequestBody List<KpiConfigDTO> kpiList, @RequestHeader("token") String token) {
		try {
			// List<KpiConfigDTO> newList = kpiList;

			List<KpiConfigDTO> newList = kpiList.stream().map(kpiConfigDTO -> {
				kpiConfigDTO.setUpdateUserId(accountApi.parseTokenId(token));
				return kpiConfigDTO;
			}).collect(Collectors.toList());
			return kpiConfigService.batchSave(newList);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR, e.getMessage());
		}
	}

}
