package com.qunzhixinxi.hnqz.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmDaCustomer;
import com.qunzhixinxi.hnqz.admin.entity.base.BaseEntity;
import com.qunzhixinxi.hnqz.admin.enums.TenantStatus;
import com.qunzhixinxi.hnqz.admin.service.WmDaCustomerService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 档案-客户管理
 *
 * @author lixuesong
 * @date 2021年06月04日 15:44
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wmDaCustomer")
@Api(value = "wmDaCustomer", tags = "档案-客户管理")
public class WmDaCustomerController {

	private WmDaCustomerService wmDaCustomerService;

	/**
	 * 查看客户开发进度
	 *
	 * @param page
	 * @param wmDaCustomer
	 * @return
	 */
	@GetMapping("/listProgess")
	public R listWmDaCustomerProgessPage(Page page, WmDaCustomer wmDaCustomer) {
		try {
			// 拼接查询条件
			LambdaQueryWrapper<WmDaCustomer> queryWrapper = Wrappers.lambdaQuery();
			if (wmDaCustomer != null) {
				// 客户id
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerNumber())) {
					queryWrapper.eq(WmDaCustomer::getCustomerNumber, wmDaCustomer.getCustomerNumber());
				}
				// 客户名称
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerName())) {
					queryWrapper.like(WmDaCustomer::getCustomerName, wmDaCustomer.getCustomerName());
				}
				// 客户类别
				if (wmDaCustomer.getCustomerType() != null) {
					queryWrapper.eq(WmDaCustomer::getCustomerType, wmDaCustomer.getCustomerType());
				}
				// 客户经理
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerManager())) {
					queryWrapper.like(WmDaCustomer::getCustomerManager, wmDaCustomer.getCustomerManager());
				}
				// 开发进度
				if (wmDaCustomer.getCustomerStatus() != null) {
					queryWrapper.like(WmDaCustomer::getCustomerStatus, wmDaCustomer.getCustomerStatus());
				}
			}
			// 如果为上线状态，则只获取当月的
			LocalDate now = LocalDate.now();
			LocalDateTime startDay = now.withDayOfMonth(1).atStartOfDay();
			LocalDateTime endDay = now.plusMonths(1).atStartOfDay().minusSeconds(1);
			queryWrapper.and(wrapper ->
					wrapper.eq(WmDaCustomer::getCustomerStatus, TenantStatus.GOING_ONLINE)
					.between(WmDaCustomer::getCreateTime, startDay, endDay)
					.or()
					.ne(WmDaCustomer::getCustomerStatus, TenantStatus.GOING_ONLINE)
					.ne(WmDaCustomer::getCustomerStatus, TenantStatus.OFFLINE))
					.orderByDesc(WmDaCustomer::getCreateTime);

			Page queryPage = wmDaCustomerService.page(page, queryWrapper);
			return R.ok(queryPage);
		} catch (Exception e) {
			log.error("查询失败", e);
		}
		return R.failed("查询失败");
	}

	/**
	 * 查看客户列表
	 *
	 * @param page
	 * @param wmDaCustomer
	 * @return
	 */
	@GetMapping("/list")
	public R listWmDaCustomerPage(Page page, WmDaCustomer wmDaCustomer) {
		try {
			return R.ok(wmDaCustomerService.listCustomerPage(page, wmDaCustomer));
		} catch (Exception e) {
			log.error("查询失败", e);
		}
		return R.failed("查询失败");
	}

	/**
	 * 根据id查询客户
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R getWmDaCustomerById(@PathVariable("id") Integer id) {
		if (id == null) {
			return R.failed("id不能为空");
		}
		return R.ok(wmDaCustomerService.getCustomerById(id));
	}

	/**
	 * 保存客户
	 *
	 * @param wmDaCustomer
	 * @return
	 */
	@PostMapping
	public R saveWmDaCustomer(@RequestBody @Validated({BaseEntity.Create.class}) WmDaCustomer wmDaCustomer, BindingResult result) {
		try {
			if (result.hasErrors()) {
				return R.failed(result.getFieldError().getDefaultMessage());
			}
			wmDaCustomerService.saveCustomer(wmDaCustomer);
			return R.ok();
		} catch (Exception e) {
			log.error("新增失败", e);
		}
		return R.failed("新增失败");
	}

	/**
	 * 修改客户
	 * @param wmDaCustomer
	 * @return
	 */
	@PutMapping
	public R updateWmDaCustomer(@RequestBody @Validated({BaseEntity.Update.class}) WmDaCustomer wmDaCustomer, BindingResult result) {
		try {
			if (result.hasErrors()) {
				return R.failed(result.getFieldError().getDefaultMessage());
			}
			wmDaCustomerService.updateCustomerById(wmDaCustomer);
			return R.ok();
		} catch (Exception e) {
			log.error("更新失败", e);
		}
		return R.failed("更新失败");
	}

	/**
	 * 删除客户
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	public R deleteWmDaCustomer(@PathVariable("id") Integer id) {
		try {
			if (id == null) {
				return R.failed("id不能为空");
			}
			wmDaCustomerService.removeById(id);
			return R.ok();
		} catch (Exception e) {
			log.error("删除失败", e);
		}
		return R.failed("删除失败");
	}

	/**
	 * 导出指定月的统计列表
	 *
	 * @param wmDaCustomer
	 * @return
	 */
	@GetMapping("/export")
	public R exportMultiMonthStatistics(WmDaCustomer wmDaCustomer) {
		try {
			// 拼接查询条件
			LambdaQueryWrapper<WmDaCustomer> queryWrapper = Wrappers.lambdaQuery();
			if (wmDaCustomer != null) {
				// 客户名称
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerName())) {
					queryWrapper.like(WmDaCustomer::getCustomerName, wmDaCustomer.getCustomerName());
				}
				// 客户类别
				if (wmDaCustomer.getCustomerType() != null) {
					queryWrapper.eq(WmDaCustomer::getCustomerType, wmDaCustomer.getCustomerType());
				}
				// 客户经理
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerManager())) {
					queryWrapper.like(WmDaCustomer::getCustomerManager, wmDaCustomer.getCustomerManager());
				}
				// 开发进度
				if (wmDaCustomer.getCustomerStatus() != null) {
					queryWrapper.eq(WmDaCustomer::getCustomerStatus, wmDaCustomer.getCustomerStatus());
				}
			}
			// 如果为上线状态，则只获取当月的
			LocalDate now = LocalDate.now();
			LocalDateTime startDay = now.withDayOfMonth(1).atStartOfDay();
			LocalDateTime endDay = now.plusMonths(1).atStartOfDay().minusSeconds(1);
			queryWrapper.and(wrapper ->
					wrapper.eq(WmDaCustomer::getCustomerStatus, TenantStatus.GOING_ONLINE)
							.between(WmDaCustomer::getCreateTime, startDay, endDay)
							.or()
							.ne(WmDaCustomer::getCustomerStatus, TenantStatus.GOING_ONLINE))
					.orderByDesc(WmDaCustomer::getCreateTime);
			List<WmDaCustomer> list = wmDaCustomerService.list(queryWrapper);
			// 导出excel
			return wmDaCustomerService.exportCustomer(list);
		} catch (Exception e) {
			log.error("导出失败", e);
		}
		return R.failed("导出失败");
	}

}
