package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.entity.OrganizationCourseUnitPriceSettings;
import com.ym.mec.biz.dal.page.OrganizationCourseUnitPriceSettingsQueryInfo;
import com.ym.mec.biz.service.OrganizationCourseUnitPriceSettingsService;
import com.ym.mec.common.controller.BaseController;

@RequestMapping("organizationCourseUnitPriceSettings")
@Api(tags = "分部课程单价设置服务")
@RestController
public class OrganizationCourseUnitPriceSettingsController extends BaseController {

	@Autowired
	private OrganizationCourseUnitPriceSettingsService organizationCourseUnitPriceSettingsService;

	@ApiOperation(value = "分页查询列表")
	@GetMapping(value = "/queryPage", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('organizationCourseUnitPriceSettings/queryPage')")
	public Object queryPage(OrganizationCourseUnitPriceSettingsQueryInfo queryInfo) {
		return succeed(organizationCourseUnitPriceSettingsService.queryPage(queryInfo));
	}

	@ApiOperation(value = "新增对象")
	@PostMapping(value = "/insert", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('organizationCourseUnitPriceSettings/insert')")
	public Object insert(@RequestBody List<OrganizationCourseUnitPriceSettings> organizationCourseUnitPriceSettingsList) {
		for (OrganizationCourseUnitPriceSettings organizationCourseUnitPriceSettings : organizationCourseUnitPriceSettingsList) {
			OrganizationCourseUnitPriceSettings originalObj = organizationCourseUnitPriceSettingsService.queryByOrganIdAndCourseTypeAndChargeType(
					organizationCourseUnitPriceSettings.getOrganId(), organizationCourseUnitPriceSettings.getCourseType(),
					organizationCourseUnitPriceSettings.getChargeTypeId());
			if (originalObj != null) {
				return failed("当前分部已经存在该课程类型的单价");
			}
		}
		organizationCourseUnitPriceSettingsService.batchInsert(organizationCourseUnitPriceSettingsList);
		return succeed();
	}

	@ApiOperation(value = "修改对象")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@PreAuthorize("@pcs.hasPermissions('organizationCourseUnitPriceSettings/update')")
	public Object update(@RequestBody OrganizationCourseUnitPriceSettings organizationCourseUnitPriceSettings) {
		organizationCourseUnitPriceSettings.setUpdateTime(new Date());
		organizationCourseUnitPriceSettingsService.update(organizationCourseUnitPriceSettings);
		return succeed();
	}

	@ApiOperation(value = "删除收费类型")
	@PostMapping("/del/{id}")
	@PreAuthorize("@pcs.hasPermissions('organizationCourseUnitPriceSettings/del')")
	public Object del(@ApiParam(value = "编号", required = true) @PathVariable("id") Integer id) {
		organizationCourseUnitPriceSettingsService.delete(id);
		return succeed();
	}

}
