package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.SporadicChargeInfo;
import com.ym.mec.biz.dal.enums.SporadicChargeTypeEnum;
import com.ym.mec.biz.dal.page.SporadicChargeInfoQueryInfo;
import com.ym.mec.biz.service.SporadicChargeInfoService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RequestMapping("sporadicChargeInfo")
@Api(tags = "零星收费管理")
@RestController
public class SporadicChargeInfoController extends BaseController {

	@Autowired
	private SporadicChargeInfoService sporadicChargeInfoService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private EmployeeDao employeeDao;

	@ApiOperation(value = "查询零星收费列表")
	@GetMapping("/queryPage")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/queryPage')")
	public Object queryPage(SporadicChargeInfoQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		if(!sysUser.getIsSuperAdmin()){
			Employee employee = employeeDao.get(sysUser.getId());
			if (StringUtils.isEmpty(queryInfo.getOrganId())) {
				queryInfo.setOrganId(employee.getOrganIdList());
			}else if(StringUtils.isEmpty(employee.getOrganIdList())){
				return failed("用户所在分部异常");
			}else {
				List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
				if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
					return failed("非法请求");
				}
			}
		}
		queryInfo.setSort("id_");
		return succeed(sporadicChargeInfoService.queryDetailPage(queryInfo));
	}

	@ApiOperation(value = "单查询")
	@GetMapping("/get")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/get')")
	public Object get(Integer id) {
		return succeed(sporadicChargeInfoService.getDetail(id));
	}

	@ApiOperation(value = "新增")
	@PostMapping("/add")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/add')")
	public Object add(SporadicChargeInfo sporadicChargeInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		sporadicChargeInfo.setOperatorId(sysUser.getId());
		if(sporadicChargeInfo.getUserId() != null){
			SysUser user = sysUserFeignService.queryUserById(sporadicChargeInfo.getUserId());
			if(user == null){
				return failed("学员信息不存在");
			}
			if(!user.getUserType().contains("STUDENT")){
				return failed("该用户不是学员");
			}
			if(user.getOrganId() == null){
				return failed("分部信息异常");
			}
			sporadicChargeInfo.setOrganId(user.getOrganId());
		}
		if(sporadicChargeInfo.getChargeType().equals(SporadicChargeTypeEnum.LUCKY_BAG_ACTIVE)){
			SporadicChargeInfo info = sporadicChargeInfoService.findByOrganIdAndType(sporadicChargeInfo.getOrganId(), sporadicChargeInfo.getChargeType().getCode());
			if(info != null){
				return failed("分部活动已存在，请勿重复创建");
			}
		}
		if (sporadicChargeInfo.getChargeType().equals(SporadicChargeTypeEnum.DOUBLE_ELEVEN2020)) {
			List<SporadicChargeInfo> organActiveInfo = sporadicChargeInfoService.getOrganActiveInfo(sporadicChargeInfo.getOrganId(), sporadicChargeInfo.getChargeType().getCode());
			if (organActiveInfo.size() >= 2) {
				return failed("分部活动已存在，请勿重复创建");
			}
		}
		return succeed(sporadicChargeInfoService.insert(sporadicChargeInfo));
	}

	@ApiOperation(value = "修改")
	@PostMapping("/update")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/update')")
	public Object update(SporadicChargeInfo sporadicChargeInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		SporadicChargeInfo info = sporadicChargeInfoService.get(sporadicChargeInfo.getId());
		sporadicChargeInfo.setVersion(info.getVersion());
		sporadicChargeInfo.setOperatorId(sysUser.getId());
		sporadicChargeInfo.setUpdateTime(new Date());
		int update = sporadicChargeInfoService.update(sporadicChargeInfo);
		if (update <= 0) {
			return failed("修改失败，请重试");
		}
		return succeed(update);
	}

	@ApiOperation(value = "开启/关闭缴费")
	@PostMapping("/updateOpenFlag")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/updateOpenFlag')")
	public Object updateOpenFlag(Integer sporadicId,Integer openFlag) {
		sporadicChargeInfoService.updateOpenFlag(sporadicId,openFlag);
		return succeed();
	}

	@ApiOperation(value = "删除")
	@PostMapping("/delete")
	@PreAuthorize("@pcs.hasPermissions('sporadicChargeInfo/delete')")
	public Object delete(Integer id) {
		sporadicChargeInfoService.del(id);
		return succeed();
	}
}
