package org.springblade.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.dto.YksytChannelSpidDTO;
import org.springblade.finance.entity.YksytChannelSpid;
import org.springblade.finance.entity.YksytChannelSpidToCheck;
import org.springblade.finance.entity.YksytChannels;
import org.springblade.finance.service.IYksytChannelSpidService;
import org.springblade.finance.service.IYksytChannelSpidToCheckService;
import org.springblade.finance.service.IYksytChannelsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 其它结算渠道产品审核表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2022-04-07
 */
@RestController
@RequestMapping("finance/yksytSpidToCheck")
@Api(value = "优酷收银台渠道产品审核管理", tags = "优酷收银台渠道产品审核管理接口")
@Slf4j
public class YksytChannelSpidToCheckController {

	@Autowired
	private IYksytChannelSpidToCheckService yksytChannelSpidToCheckService;

	@Autowired
	private IYksytChannelsService yksytChannelsService;

	@Autowired
	private IYksytChannelSpidService yksytChannelSpidService;


	@PostMapping("/save")
	@ApiOperation(value = "新增优酷收银台渠道产品")
	public R save(@RequestBody @Valid YksytChannelSpidDTO channelSpid){
		String userId = SecureUtil.getUserId().toString();
		//常林 和 曹磊 正式环境
		/*if(!userId.equals("1403233023518834690") && !userId.equals("1392419712524816386") ){
			return R.fail("您没有录入权限");
		}*/

		//明亮 和 小强 测试环境
		log.info("新增渠道产品 channelSpid = {} userId = {}",channelSpid,userId);
		if(userId.equals("1411866408753176578") || userId.equals("1392663744517726210")){}else {
			return R.fail("您没有录入权限");
		}

		if(channelSpid.getDmonths() < 0 || channelSpid.getDmonths() > 6){
			return R.fail("去重月份数不规范");
		}

		List<YksytChannels> oldChannel = yksytChannelsService.list(new LambdaQueryWrapper<YksytChannels>()
			.eq(YksytChannels::getChannelid, channelSpid.getChannelid())
			.eq(YksytChannels::getChannelname,channelSpid.getChannelname())
			.eq(YksytChannels::getCompany,channelSpid.getCompany())
		);
		if(!oldChannel.isEmpty() && oldChannel.size() > 0){
			List<YksytChannelSpid> list = yksytChannelSpidService.list(new LambdaQueryWrapper<YksytChannelSpid>()
				.eq(YksytChannelSpid::getChannelid, channelSpid.getChannelid())
				.eq(YksytChannelSpid::getSpid, channelSpid.getSpid())
				.eq(YksytChannelSpid::getIsValid, channelSpid.getIsValid())
			);
			if(list != null && list.size() > 0){
				return R.fail("数据已存在");
			}else {
				YksytChannelSpidToCheck entity = new YksytChannelSpidToCheck();
				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");
				}


				//常林提交 刘蓓蓓审核
				/*if(userId.equals("1403233023518834690")){
					entity.setChecker("1425706030178791425");
					//曹磊提交 潘宁审核
				}else if(userId.equals("1392419712524816386")){
					entity.setChecker("1409422542916128770");
				}*/
				entity.setCheckStatus(0);
				entity.setIsValid(1);
				boolean sa = yksytChannelSpidToCheckService.save(entity);
				if(sa){
					//intChannelSpidToCheckService.sysSpname();
					return R.success("添加渠道产品成功");
				}
				return R.fail("添加渠道产品失败");
			}
		}else {
			List<YksytChannels> channel = yksytChannelsService.list(new LambdaQueryWrapper<YksytChannels>()
				.eq(YksytChannels::getChannelid, channelSpid.getChannelid())
				.or()
				.eq(YksytChannels::getChannelname,channelSpid.getChannelname())
				.or()
				.eq(YksytChannels::getCompany,channelSpid.getCompany())
			);
			if(channel.isEmpty() && channel.size() == 0){
				YksytChannelSpidToCheck entity = new YksytChannelSpidToCheck();
				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");
				}



				//常林提交 刘蓓蓓审核
				/*if(userId.equals("1403233023518834690")){
					entity.setChecker("1425706030178791425");
					//曹磊提交 潘宁审核
				}else if(userId.equals("1392419712524816386")){
					entity.setChecker("1409422542916128770");
				}*/
				entity.setCheckStatus(0);
				entity.setIsValid(1);
				boolean sa = yksytChannelSpidToCheckService.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<YksytChannelSpid> list = yksytChannelSpidService.list(new LambdaQueryWrapper<YksytChannelSpid>()
						.eq(YksytChannelSpid::getChannelid, channelSpid.getChannelid())
						.eq(YksytChannelSpid::getSpid, channelSpid.getSpid())
						.eq(YksytChannelSpid::getIsValid, channelSpid.getIsValid())
					);
					if(list != null && list.size() > 0){
						return R.fail("数据已存在");
					}else {
						YksytChannelSpidToCheck entity = new YksytChannelSpidToCheck();
						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");
						}

						//常林提交 刘蓓蓓审核
						/*if(userId.equals("1403233023518834690")){
							entity.setChecker("1425706030178791425");
							//曹磊提交 潘宁审核
						}else if(userId.equals("1392419712524816386")){
							entity.setChecker("1409422542916128770");
						}*/
						entity.setCheckStatus(0);
						entity.setIsValid(1);
						boolean sa = yksytChannelSpidToCheckService.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<YksytChannelSpidToCheck> list = yksytChannelSpidToCheckService.list(new LambdaQueryWrapper<YksytChannelSpidToCheck>()
			.eq(YksytChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(YksytChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(YksytChannelSpidToCheck::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 = yksytChannelSpidToCheckService.update(list.get(0), new LambdaUpdateWrapper<YksytChannelSpidToCheck>()
			.eq(YksytChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(YksytChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(YksytChannelSpidToCheck::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<YksytChannelSpidToCheck> list = yksytChannelSpidToCheckService.list(new LambdaQueryWrapper<YksytChannelSpidToCheck>()
			.eq(YksytChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(YksytChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(YksytChannelSpidToCheck::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 = yksytChannelSpidToCheckService.update(list.get(0), new LambdaUpdateWrapper<YksytChannelSpidToCheck>()
			.eq(YksytChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(YksytChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(YksytChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

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

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

			if(oldChannel == null || oldChannel.size() == 0){
				boolean sa = yksytChannelsService.save(intChannels);
				if(sa){
					YksytChannelSpid intChannelSpid = new YksytChannelSpid();
					BeanUtils.copyProperties(list.get(0),intChannelSpid);
					yksytChannelSpidService.save(intChannelSpid);
					return R.success("审核成功");
				}
			}else {
				YksytChannelSpid intChannelSpid = new YksytChannelSpid();
				BeanUtils.copyProperties(list.get(0),intChannelSpid);
				yksytChannelSpidService.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<YksytChannelSpidToCheck> list = yksytChannelSpidToCheckService.list(new LambdaQueryWrapper<YksytChannelSpidToCheck>()
			.eq(YksytChannelSpidToCheck::getChannelid, dto.getChannelid())
			.eq(YksytChannelSpidToCheck::getSpid, dto.getSpid())
			.eq(YksytChannelSpidToCheck::getIsValid, dto.getIsValid())
		);

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

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

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



}

