package org.springblade.customer.controller;


import java.util.List;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.customer.entity.CustomerReport;
import org.springblade.customer.entity.FctCsIrregular;
import org.springblade.customer.entity.FctCsIrregularMoney;
import org.springblade.customer.entity.FctCsRecords;
import org.springblade.customer.entity.FctPrmViolation;
import org.springblade.customer.enums.ImportsEnum;
import org.springblade.customer.query.CustomerReportQuery;
import org.springblade.customer.service.ICustomerReportService;
import org.springblade.customer.service.IFctCsIrregularMoneyService;
import org.springblade.customer.service.IFctCsIrregularService;
import org.springblade.customer.service.IFctCsRecordsService;
import org.springblade.customer.service.IFctPrmViolationService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 客服数据表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-10-20
 */

@RestController
@RequestMapping("/customerReport")
@AllArgsConstructor
@Api(value = "客服数据管理", tags = "客服数据管理")
public class CustomerReportController {


	@Autowired
	private ICustomerReportService customerReportService;

	/**
	 * 客服投诉明细
	 */
	@Autowired
	private IFctCsIrregularService fctCsIrregularService;

	/**
	 * 呼叫中心
	 */
	@Autowired
	private IFctCsRecordsService fctCsRecordsService;

	/**
	 * PRM违约明细
	 */
	@Autowired
	private IFctPrmViolationService fctPrmViolationService;

	/**
	 * 不规范扣款
	 */
	@Autowired
	private IFctCsIrregularMoneyService fctCsIrregularMoneyService;


	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入customerReport")
	public R save(@Valid @RequestBody CustomerReport customerReport) {
		return R.status(customerReportService.save(customerReport));
	}


	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入id")
	public R remove(@RequestParam String id) {
		CustomerReport report = customerReportService.getById(id);
		if(report != null){
			String batchNum = report.getBatchNum();
			String categoryCode = report.getCategoryCode();
			//客服投诉明细报表导入
			if (ImportsEnum.FCT_KS.getCode().equals(categoryCode)) {
				fctCsIrregularService.remove(new LambdaQueryWrapper<FctCsIrregular>()
					.eq(FctCsIrregular::getBatchNum,batchNum)
				);
			}
			//呼叫中心报表导入
			if (ImportsEnum.FCT_HJ.getCode().equals(categoryCode)) {
				fctCsRecordsService.remove(new LambdaQueryWrapper<FctCsRecords>()
					.eq(FctCsRecords::getBatchNum,batchNum)
				);
			}
			//PRM违约明细报表导入
			if (ImportsEnum.FCT_WY.getCode().equals(categoryCode)) {
				fctPrmViolationService.remove(new LambdaQueryWrapper<FctPrmViolation>()
					.eq(FctPrmViolation::getBatchNum,batchNum)
				);
			}
			//不规范扣款报表导入
			if (ImportsEnum.FCT_KK.getCode().equals(categoryCode)) {
				fctCsIrregularMoneyService.remove(new LambdaQueryWrapper<FctCsIrregularMoney>()
					.eq(FctCsIrregularMoney::getBatchNum,batchNum)
				);
			}
		}
		return R.status(customerReportService.removeById(id));
	}


	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入customerReport")
	public R update(@Valid @RequestBody CustomerReport customerReport) {
		return R.status(customerReportService.updateById(customerReport));
	}


	@GetMapping("/list")
	@ApiOperation(value = "列表")
	public R<List<CustomerReport>> list() {
		List<CustomerReport> list = customerReportService.list();
		return R.data(list);
	}


	@GetMapping("/page")
	@ApiOperation(value = "分页")
	public R<IPage<CustomerReport>> page(CustomerReportQuery pageQuery, Query query) {
		IPage<CustomerReport> page = Condition.getPage(query);
		IPage<CustomerReport> pages = customerReportService.getPageByQuery(page, pageQuery);
		return R.data(pages);
	}

}

