package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.WmScorePackageSettleNote;
import com.qunzhixinxi.hnqz.admin.entity.model.excel.WmSettlePackageSettleExcelModel;
import com.qunzhixinxi.hnqz.admin.entity.output.WmTaskSettleNoteOutput;
import com.qunzhixinxi.hnqz.admin.enums.SettleStatusEnum;
import com.qunzhixinxi.hnqz.admin.enums.SubjectTypeEnum;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageSettleNoteService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;


/**
 * @author
 * @date 2021-01-26 15:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/settleNote")
@Api(value = "settleNote", tags = "管理")
public class WmScorePackageSettleNoteController {

	private final WmScorePackageSettleNoteService wmScorePackageSettleNoteService;
	private final WmScorePackageService wmScorePackageService;

	/**
	 * 分页查询任务+结算信息
	 *
	 * @param page 分页对象
	 * @param note
	 * @return
	 */
	@ApiOperation(value = "分页查询任务+结算信息", notes = "分页查询任务+结算信息")
	@GetMapping("/info/page")
	public R getWmTaskSettleInfoPage(Page page, WmScorePackageSettleNote note) {
		if (StringUtils.isEmpty(note.getPackageId()) || StringUtils.isEmpty(note.getUserId())) {

			return R.failed("参数异常");
		}
		// 以id倒序排列
		page.addOrder(OrderItem.desc("id"));
		return R.ok(wmScorePackageSettleNoteService.getWmTaskSettleInfoPage(page, note));
	}

	/**
	 * 分页查询任务+结算信息
	 *
	 * @param page 分页对象
	 * @param note
	 * @return
	 */
	@ApiOperation(value = "分页查询任务+结算信息", notes = "分页查询任务+结算信息")
	@GetMapping("/settle/page")
	public R getWmTaskForSettlePage(Page page, WmScorePackageSettleNote note) {
		if (StringUtils.isEmpty(note.getPackageId())) {
			return R.failed("参数异常");
		}
		if (note.getStartSubTime() != null && note.getEndSubTime() != null) {
			// 开始时间精确到0:00:00
			LocalDateTime startSubTime = LocalDateTime.of(note.getStartSubTime().toLocalDate(), LocalTime.MIN);
			// 结束时间精确到23:59:59.999999999
			LocalDateTime endSubTime = LocalDateTime.of(note.getEndSubTime().toLocalDate(), LocalTime.MAX);
			note.setStartSubTime(startSubTime);
			note.setEndSubTime(endSubTime);
		}
		return R.ok(wmScorePackageSettleNoteService.getWmTaskForSettlePage(page, note));
	}

	/**
	 * 导出结算包结算-结算数据
	 *
	 * @param note
	 * @return
	 */
	@ResponseExcel(name = "exportWmTaskForSettle", sheet = "结算包结算数据")
	@ApiOperation(value = "导出结算包结算-结算数据", notes = "导出结算包结算-结算数据")
	@GetMapping("/settle/export")
	public List<WmSettlePackageSettleExcelModel> exportWmTaskForSettle(WmScorePackageSettleNote note) {
		if (StringUtils.isEmpty(note.getPackageId())) {
			return new ArrayList<>();
		}
		if (note.getStartSubTime() != null && note.getEndSubTime() != null) {
			// 开始时间精确到0:00:00
			LocalDateTime startSubTime = LocalDateTime.of(note.getStartSubTime().toLocalDate(), LocalTime.MIN);
			// 结束时间精确到23:59:59.999999999
			LocalDateTime endSubTime = LocalDateTime.of(note.getEndSubTime().toLocalDate(), LocalTime.MAX);
			note.setStartSubTime(startSubTime);
			note.setEndSubTime(endSubTime);
		}
		List<WmTaskSettleNoteOutput> list = wmScorePackageSettleNoteService.listWmTaskForSettle(note);
		if (CollectionUtil.isEmpty(list)) {
			return new ArrayList<>();
		}
		// 转换为导出实体
		WmScorePackage scorePackage = wmScorePackageService.getById(note.getPackageId());
		List<WmSettlePackageSettleExcelModel> exportList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			WmTaskSettleNoteOutput settleNoteOutput = list.get(i);
			WmSettlePackageSettleExcelModel excelModel = new WmSettlePackageSettleExcelModel();
			excelModel.setNumber(i + 1);
			excelModel.setScorePackageName(scorePackage.getScorePackageName());
			excelModel.setRealName(settleNoteOutput.getRealName());
			excelModel.setUsername(settleNoteOutput.getUsername());
			excelModel.setTaskTypeName(settleNoteOutput.getTaskTypeName());
			excelModel.setXmbm(settleNoteOutput.getXmbm());
			excelModel.setCreateTime(settleNoteOutput.getCreateTime());
			excelModel.setSettleAmount(settleNoteOutput.getSettleAmount());
			excelModel.setCategoryName(settleNoteOutput.getCategoryName());
			excelModel.setSubType(SubjectTypeEnum.resolve(settleNoteOutput.getSubType()).getDesc());
			excelModel.setSubTime(settleNoteOutput.getSubTime());
			excelModel.setNotifyTime(settleNoteOutput.getNotifyTime());
			excelModel.setSettleNoteStatus(SettleStatusEnum.resolve(String.valueOf(settleNoteOutput.getSettleNoteStatus())).getName2());
			exportList.add(excelModel);
		}

		return exportList;
	}

	/**
	 * 特定任务
	 * 查询生成结算信息
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "查询生成结算信息", notes = "查询生成结算信息")
	@PostMapping("/getSettleInfo")
	public R getSettleInfo(@RequestBody WmScorePackageSettleNote note) {
		return wmScorePackageSettleNoteService.getSettleInfo(note);
	}

	/**
	 * 保存
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "保存", notes = "保存")
	@PostMapping("/saveSettleInfo")
	public R saveSettleInfo(@RequestBody WmScorePackageSettleNote note) {
//		if(null == note.getId()){
//			return R.failed("参数异常");
//		}
		return wmScorePackageSettleNoteService.saveSettleInfo(note);
	}

	/**
	 * 提交结算申请
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "提交结算申请", notes = "提交结算申请")
	@PostMapping("/subSettleInfo")
	public R subSettleInfo(@RequestBody WmScorePackageSettleNote note) {

		return wmScorePackageSettleNoteService.subSettleInfo(note);
	}

	/**
	 * 撤销结算申请
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "撤销结算申请", notes = "撤销结算申请")
	@PostMapping("/revokeSettle")
	public R revokeSettle(@RequestBody WmScorePackageSettleNote note) {
//		if(null == note.getId()){
//			return R.failed("参数异常");
//		}
		return wmScorePackageSettleNoteService.revokeSettle(note);
	}

	/**
	 * 选择渠道结算单一结算
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "选择渠道结算单一结算", notes = "选择渠道结算单一结算")
	@PostMapping("/singletSettle")
	public R singletSettle(@RequestBody WmScorePackageSettleNote note) {
		if (null == note.getId()) {
			return R.failed("参数异常");
		}
		return wmScorePackageSettleNoteService.singletSettle(note);
	}

	/**
	 * 提交结算申请
	 *
	 * @param note
	 * @return R
	 */
	@ApiOperation(value = "提交结算申请", notes = "提交结算申请")
	@PostMapping("/batchSettle")
	public R batchSettle(@RequestBody WmScorePackageSettleNote note) {
		if (StringUtils.isEmpty(note.getIdStr())) {
			return R.failed("参数异常");
		}
		return wmScorePackageSettleNoteService.batchSettle(note);
	}


	/**
	 * 分页查询
	 *
	 * @param page                     分页对象
	 * @param wmScorePackageSettleNote
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getWmScorePackageSettleNotePage(Page page, WmScorePackageSettleNote wmScorePackageSettleNote) {
		return R.ok(wmScorePackageSettleNoteService.page(page, Wrappers.query(wmScorePackageSettleNote)));
	}


	/**
	 * 通过id查询
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Integer id) {
		return R.ok(wmScorePackageSettleNoteService.getById(id));
	}

	/**
	 * 新增
	 *
	 * @param wmScorePackageSettleNote
	 * @return R
	 */
	@ApiOperation(value = "新增", notes = "新增")
	@SysLog("新增")
	@PostMapping
	public R save(@RequestBody WmScorePackageSettleNote wmScorePackageSettleNote) {
		return R.ok(wmScorePackageSettleNoteService.save(wmScorePackageSettleNote));
	}

	/**
	 * 修改
	 *
	 * @param wmScorePackageSettleNote
	 * @return R
	 */
	@ApiOperation(value = "修改", notes = "修改")
	@SysLog("修改")
	@PutMapping
	public R updateById(@RequestBody WmScorePackageSettleNote wmScorePackageSettleNote) {
		return R.ok(wmScorePackageSettleNoteService.updateById(wmScorePackageSettleNote));
	}

	/**
	 * 通过id删除
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除", notes = "通过id删除")
	@SysLog("通过id删除")
	@DeleteMapping("/{id}")
	public R removeById(@PathVariable Integer id) {
		return R.ok(wmScorePackageSettleNoteService.removeById(id));
	}

}
