package org.springblade.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.finance.dto.ChannelSpidDTO;
import org.springblade.finance.dto.ChannelSpidVerifyDTO;
import org.springblade.finance.entity.*;
import org.springblade.finance.enums.AuditStatusEnum;
import org.springblade.finance.service.IIntChannelSpidService;
import org.springblade.finance.service.IIntChannelSpidToCheckService;
import org.springblade.finance.service.IIntChannelsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 互联网结算渠道产品审核表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-12-20
 */
@RestController
@RequestMapping("finance/SpidToCheck")
@Api(value = "渠道产品审核管理", tags = "渠道产品审核管理接口")
@Slf4j
public class IntChannelSpidToCheckController {

	@Autowired
	private IIntChannelSpidToCheckService intChannelSpidToCheckService;

	@Autowired
	private IIntChannelsService intChannelsService;

	@Autowired
	private IIntChannelSpidService intChannelSpidService;


	@PostMapping("/save")
	@ApiOperation(value = "新增渠道产品")
	public R save(@RequestBody @Valid ChannelSpidDTO channelSpid){
		String userId = SecureUtil.getUserId().toString();
		log.info("新增渠道产品 channelSpid = {} userId = {}",channelSpid,userId);
		if(userId.equals("1411866408753176578") || userId.equals("1392663744517726210")){}else {
			return R.fail("您没有录入权限");
		}
		List<IntChannels> oldChannel = intChannelsService.list(new LambdaQueryWrapper<IntChannels>()
			.eq(IntChannels::getChannelid, channelSpid.getChannelid())
			.eq(IntChannels::getChannelname,channelSpid.getChannelname())
			.eq(IntChannels::getCompany,channelSpid.getCompany())
		);
		if(!oldChannel.isEmpty() && oldChannel.size() > 0){
			List<IntChannelSpid> list = intChannelSpidService.list(new LambdaQueryWrapper<IntChannelSpid>()
				.eq(IntChannelSpid::getChannelid, channelSpid.getChannelid())
				.eq(IntChannelSpid::getSpid, channelSpid.getSpid())
				.eq(IntChannelSpid::getIsValid, channelSpid.getIsValid())
			);
			if(list != null && list.size() > 0){
				return R.fail("数据已存在");
			}else {
				IntChannelSpidToCheck entity = new IntChannelSpidToCheck();
				BeanUtils.copyProperties(channelSpid,entity);
				entity.setInputter(userId);
				entity.setInputTime(new Date());
				entity.setDataStartdate(new Date());
				//明亮提交 小强审核
				if(userId.equals("1411866408753176578")){
					entity.setChecker("1392663744517726210");
					//小强提交 明亮审核
				}else if(userId.equals("1392663744517726210")){
					entity.setChecker("1411866408753176578");
				}
				entity.setCheckStatus(0);
				boolean sa = intChannelSpidToCheckService.save(entity);
				if(sa){
					intChannelSpidToCheckService.sysSpname();
					return R.success("添加渠道产品成功");
				}
				return R.fail("添加渠道产品失败");
			}
		}else {
			List<IntChannels> channel = intChannelsService.list(new LambdaQueryWrapper<IntChannels>()
				.eq(IntChannels::getChannelid, channelSpid.getChannelid())
				.or()
				.eq(IntChannels::getChannelname,channelSpid.getChannelname())
				.or()
				.eq(IntChannels::getCompany,channelSpid.getCompany())
			);
			if(channel.isEmpty() && channel.size() == 0){
				IntChannelSpidToCheck entity = new IntChannelSpidToCheck();
				BeanUtils.copyProperties(channelSpid,entity);
				entity.setInputter(userId);
				entity.setInputTime(new Date());
				entity.setDataStartdate(new Date());
				//明亮提交 小强审核
				if(userId.equals("1411866408753176578")){
					entity.setChecker("1392663744517726210");
					//小强提交 明亮审核
				}else if(userId.equals("1392663744517726210")){
					entity.setChecker("1411866408753176578");
				}
				entity.setCheckStatus(0);
				boolean sa = intChannelSpidToCheckService.save(entity);
				if(sa){
					intChannelSpidToCheckService.sysSpname();
					return R.success("添加渠道产品成功");
				}
				return R.fail("添加渠道产品失败");
			}else {
				if(channel.get(0).getChannelname().equals(channelSpid.getChannelname()) || channel.get(0).getCompany().equals(channelSpid.getCompany()) || channel.get(0).getChannelid() == channelSpid.getChannelid()){
					return R.fail("渠道ID/渠道名/公司名冲突");
				}else {
					List<IntChannelSpid> list = intChannelSpidService.list(new LambdaQueryWrapper<IntChannelSpid>()
						.eq(IntChannelSpid::getChannelid, channelSpid.getChannelid())
						.eq(IntChannelSpid::getSpid, channelSpid.getSpid())
						.eq(IntChannelSpid::getIsValid, channelSpid.getIsValid())
					);
					if(list != null && list.size() > 0){
						return R.fail("数据已存在");
					}else {
						IntChannelSpidToCheck entity = new IntChannelSpidToCheck();
						BeanUtils.copyProperties(channelSpid,entity);
						entity.setInputter(userId);
						entity.setInputTime(new Date());
						entity.setDataStartdate(new Date());
						/*if(userId.equals("1403233023518834690")){
							entity.setChecker("1425706030178791425");
						}else if(userId.equals("1392419712524816386")){
							entity.setChecker("1409422542916128770");
						}*/
						//明亮提交 小强审核
						if(userId.equals("1411866408753176578")){
							entity.setChecker("1392663744517726210");
							//小强提交 明亮审核
						}else if(userId.equals("1392663744517726210")){
							entity.setChecker("1411866408753176578");
						}
						entity.setCheckStatus(0);
						boolean sa = intChannelSpidToCheckService.save(entity);
						if(sa){
							intChannelSpidToCheckService.sysSpname();
							return R.success("添加渠道产品成功");
						}
						return R.fail("添加渠道产品失败");
					}
				}
			}
		}
	}



	/**
	 * @param dto
	 * @return
	 */
	@PostMapping("/cancel")
	@ApiOperation(value = "审核驳回")
	@ApiOperationSupport(order = 6)
	public R cancel(@RequestBody @Valid ChannelSpidVerifyDTO dto){
		String userId = SecureUtil.getUserId().toString();

		List<IntChannelSpidToCheck> list = intChannelSpidToCheckService.list(new LambdaQueryWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

		if(list == null && list.size() == 0){
			return R.fail("审核数据不存在");
		}

		if(!list.get(0).getChecker().equals(userId)){
			return R.fail("您没有审核权限");
		}

		list.get(0).setCheckStatus(2);
		list.get(0).setCheckTime(new Date());
		boolean res = intChannelSpidToCheckService.update(list.get(0), new LambdaUpdateWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
		);
		return R.status(res);
	}


	/**
	 * @param dto
	 * @return
	 */
	@PostMapping("/verify")
	@ApiOperation(value = "渠道产品审核通过")
	public R verify(@RequestBody @Valid ChannelSpidVerifyDTO dto){
		String userId = SecureUtil.getUserId().toString();

		List<IntChannelSpidToCheck> list = intChannelSpidToCheckService.list(new LambdaQueryWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

		if(list == null || list.size() == 0){
			return R.fail("审核数据不存在");
		}

		if(!list.get(0).getChecker().equals(userId)){
			return R.fail("您没有审核权限");
		}


		list.get(0).setCheckStatus(1);
		list.get(0).setCheckTime(new Date());
		boolean res = intChannelSpidToCheckService.update(list.get(0), new LambdaUpdateWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

		if(res){
			IntChannels intChannels = new IntChannels();
			BeanUtils.copyProperties(list.get(0),intChannels);

			List<IntChannels> oldChannel = intChannelsService.list(new LambdaQueryWrapper<IntChannels>()
				.eq(IntChannels::getChannelid, intChannels.getChannelid())
				.eq(IntChannels::getChannelname,intChannels.getChannelname())
				.eq(IntChannels::getCompany,intChannels.getCompany())
			);

			if(oldChannel == null || oldChannel.size() == 0){
				boolean sa = intChannelsService.save(intChannels);
				if(sa){
					IntChannelSpid intChannelSpid = new IntChannelSpid();
					BeanUtils.copyProperties(list.get(0),intChannelSpid);
					intChannelSpidService.save(intChannelSpid);
					return R.success("审核成功");
				}
			}else {
				IntChannelSpid intChannelSpid = new IntChannelSpid();
				BeanUtils.copyProperties(list.get(0),intChannelSpid);
				intChannelSpidService.save(intChannelSpid);
				return R.success("审核成功");
			}
		}

		return R.fail("审核失败");
	}




	/**
	 * @param dto
	 * @return
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除")
	public R remove(@RequestBody @Valid ChannelSpidVerifyDTO dto){
		String userId = SecureUtil.getUserId().toString();

		List<IntChannelSpidToCheck> list = intChannelSpidToCheckService.list(new LambdaQueryWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

		if(list == null && list.size() == 0){
			return R.fail("删除数据不存在");
		}

		if(!list.get(0).getInputter().equals(userId)){
			return R.fail("您没有删除权限");
		}

		boolean res = intChannelSpidToCheckService.remove(new LambdaUpdateWrapper<IntChannelSpidToCheck>()
			.eq(IntChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(IntChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(IntChannelSpidToCheck::getIsValid, dto.getIsValid())
			.eq(IntChannelSpidToCheck::getInputter,userId)
			.ne(IntChannelSpidToCheck::getCheckStatus,1)
		);
		return R.status(res);
	}



}

