package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
import com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageSettleInput;
import com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageSettleOutput;
import com.qunzhixinxi.hnqz.admin.entity.model.excel.WmScorePackageSettleExcelModel;
import com.qunzhixinxi.hnqz.admin.enums.PackageFinishStatusEnum;
import com.qunzhixinxi.hnqz.admin.enums.PackageTypeEnum;
import com.qunzhixinxi.hnqz.admin.enums.SettleStatusEnum;
import com.qunzhixinxi.hnqz.admin.enums.SubjectTypeEnum;
import com.qunzhixinxi.hnqz.admin.service.WmPayOffService;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
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 java.util.ArrayList;
import java.util.List;

/**
 * 结算表
 *
 * @author Liujh
 * @date 2020-12-1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/settle" )
@Api(value = "settle", tags = "结算管理")
public class WmPayOffController {

	private final WmPayOffService wmPayOffService;
	private final WmScorePackageService wmScorePackageService;


	@ApiOperation(value = "结算回退", notes = "提交结算")
	@SysLog("提交结算" )
	@PostMapping("/settleBack")
	public R settleBack(@RequestBody WmScorePackageSettleInput input) {
		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}
		return wmPayOffService.settleBack(input);
	}

	@ApiOperation(value = "提交结算", notes = "提交结算")
	@SysLog("提交结算" )
	@PostMapping("/settleSubmit")
	public R settleSubmit(@RequestBody WmScorePackageSettleInput input) {
		if(CollectionUtils.isEmpty(input.getNotes())){
			return R.failed("数据异常");
		}

		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}
		if(null == input.getInvoiceCategory()){
			return R.failed("数据异常");
		}

		return wmPayOffService.settleSubmit(input);
	}

	@ApiOperation(value = "保存结算", notes = "保存结算")
	@SysLog("保存结算" )
	@PostMapping("/settleSave")
	public R settleSave(@RequestBody WmScorePackageSettleInput input) {
		if(CollectionUtils.isEmpty(input.getNotes())){
			return R.failed("数据异常");
		}

		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}
		if(null == input.getInvoiceCategory()){
			return R.failed("数据异常");
		}

		return wmPayOffService.settleSave(input);
	}

	/**
	 * 弃用
	 * @Modify: start
	 * @Version: v2021.4.16
	 * @Author: ryz
	 * @Date: 2021/4/28
	*/
	@ApiOperation(value = "结算", notes = "结算")
	@SysLog("结算" )
	@PostMapping("/settleIssue")
	public R settleIssue(@RequestBody WmScorePackageSettleInput input) {
		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}
		if(CollectionUtils.isEmpty(input.getNotes())){
			return R.failed("数据异常");
		}
		if(null == input.getInvoiceCategory()){
			return R.failed("数据异常");
		}
		if(StringUtils.isEmpty(input.getScorePackageName())){
			return R.failed("数据异常");
		}

		return wmPayOffService.settleIssue(input);
	}

	@ApiOperation(value = "重新结算", notes = "重新结算")
	@SysLog("重新结算" )
	@PostMapping("/settleReIssue")
	public R settleReIssue(@RequestBody WmScorePackageSettleInput input) {
		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}

//		return wmPayOffService.settleReIssue(input);
		return wmPayOffService.settleReIssueBySubType(input);
	}

	@ApiOperation(value = "结算", notes = "结算")
	@SysLog("结算" )
	@PostMapping("/settleById")
	public R settleById(@RequestBody WmScorePackageSettleInput input) {

		return wmPayOffService.settleById(input);
	}


	@ApiOperation(value = "分页查询结算", notes = "分页查询结算")
	@SysLog("分页查询结算" )
	@GetMapping("/listPackageSettleList")
	public R listPackageSettleList(Page page, WmScorePackageSettleInput input) {

		input.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() +"");
		input.setPackageFinishStatus("1");
//		input.setPackageType1("1");
//		input.setPackageType2("1");
		input.setTypeid("4");
		input.setSettleFlag("1");
		IPage<WmScorePackageSettleOutput> outputList = wmScorePackageService.listPackageSettleList(page, input);
//		for (int i = 0; i < outputList.getRecords().size(); i++) {
//			WmScorePackageSettleNote wmScorePackageSettleNote = new WmScorePackageSettleNote();
//			wmScorePackageSettleNote.setPackageId(outputList.getRecords().get(i).getId());
//			List<WmScorePackageSettleNote> wmScorePackageSettleNoteList = wmScorePackageSettleNoteService.selectByPackageId(wmScorePackageSettleNote);
//			if(wmScorePackageSettleNoteList.size()>0){
//				outputList.getRecords().get(i).setBelongDate(String.valueOf(wmScorePackageSettleNoteList.get(0).getNotifyTime()));
//			}
//		}
		return R.ok(outputList);
	}

	/**
	 * 结算包结算导出
	 *
	 * @param input
	 * @return
	 */
	@ApiOperation(value = "积分包结算导出", notes = "积分包结算导出")
	@ResponseExcel(name = "exportPackageSettle", sheet = "积分包结算数据")
	@SysLog("积分包结算导出" )
	@GetMapping("/exportPackageSettle")
	public List<WmScorePackageSettleExcelModel> exportPackageSettle(WmScorePackageSettleInput input) {
		input.setSendPackageDeptId(SecurityUtils.getUser().getDeptId() +"");
		input.setPackageFinishStatus("1");
		input.setTypeid("4");
		input.setSettleFlag("1");
		List<WmScorePackageSettleOutput> list = wmScorePackageService.listAllPackageSettle(input);
		List<WmScorePackageSettleExcelModel> exportList = new ArrayList<>();
		if (CollectionUtil.isEmpty(list)) {
			return new ArrayList<>();
		}
		// 转换为导出实体
		for (int i = 0; i < list.size(); i++) {
			WmScorePackageSettleOutput settleOutput = list.get(i);
			WmScorePackageSettleExcelModel export = new WmScorePackageSettleExcelModel();
			export.setNumber(i + 1);
			export.setScorePackageName(settleOutput.getScorePackageName());
			export.setPackageType1(PackageTypeEnum.type1Resolve(settleOutput.getPackageType1()).getName2());
			export.setPackageEntName(settleOutput.getPackageEntName());
			export.setCreateTime(settleOutput.getCreateTime());
			export.setPackageFinishStatus(PackageFinishStatusEnum.resolve(settleOutput.getPackageFinishStatus()).getName());
			export.setScore(settleOutput.getScore());

			SubjectTypeEnum subjectTypeEnum = SubjectTypeEnum.resolve(settleOutput.getSubType());
			export.setSubType(subjectTypeEnum.getDesc());
			switch (subjectTypeEnum){
				case TYPE_RENLIJIA:
					export.setInvoiceCategory(WmScorePackageSettleExcelModel.DingCategoryEnum.resolve(settleOutput.getInvoiceCategory()).getDesc());
					break;
				case TYPE_SHUIBANGYUN:
					export.setInvoiceCategory(WmScorePackageSettleExcelModel.CategoryEnum.resolve(settleOutput.getInvoiceCategory()).getDesc());
					break;
				case TYPE_NULL:
					export.setInvoiceCategory(WmScorePackageSettleExcelModel.CategoryEnum.CATEGORY_NULL.getDesc());
					break;
			}
			export.setUserNames(settleOutput.getUserNames());
			export.setSettleAmount(settleOutput.getSettleAmount());
			export.setSettleStatus(SettleStatusEnum.resolve(settleOutput.getSettleStatus()).getName());
			export.setBelongDate(settleOutput.getBelongDate());
			exportList.add(export);
		}

		return exportList;
	}

	@ApiOperation(value = "结算", notes = "结算")
	@SysLog("结算" )
	@PostMapping("/saveSettleInfo")
	public R saveSettleInfo(@RequestBody List<WmScorePackage> wsList){
		if(CollectionUtils.isEmpty(wsList)) {
			return R.failed("请选择数据");
		}else {
			for (WmScorePackage wmScorePackage : wsList) {
				if(StringUtils.isEmpty(wmScorePackage.getId())){
					return R.failed("数据异常，请联系管理员："+wmScorePackage.getScoreName());
				}
			}
		}
		try {
			return wmPayOffService.saveSettleInfo(wsList);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}

	@ApiOperation(value = "结算-自由选择渠道", notes = "结算-自由选择渠道")
	@SysLog("结算-自由选择渠道" )
	@PostMapping("/settleIssueBySubType")
	public R settleIssueBySubType(@RequestBody WmScorePackageSettleInput input) {
		if(StringUtils.isEmpty(input.getId())){
			return R.failed("数据异常");
		}
		if(StringUtils.isEmpty(input.getSubType())){
			return R.failed("数据异常");
		}
		if(CollectionUtils.isEmpty(input.getNotes())){
			return R.failed("数据异常");
		}
		if(null == input.getInvoiceCategory()){
			return R.failed("数据异常");
		}

		if(StringUtils.isEmpty(input.getScorePackageName())){
			return R.failed("数据异常");
		}

		return wmPayOffService.settleIssueBySubType(input);
	}


}
