package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmDeptTaskTypeTemplate;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
import com.qunzhixinxi.hnqz.admin.entity.base.BaseEntity;
import com.qunzhixinxi.hnqz.admin.service.WmDeptTaskTypeTemplateService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 部门任务类型模板控制层
 *
 * @author lixuesong
 * @date 2022年01月18日 11:59
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/dept-task-type-template")
public class WmDeptTaskTypeTemplateController {

	private final WmDeptTaskTypeTemplateService wmDeptTaskTypeTemplateService;

	private final WmTaskTypeService wmTaskTypeService;

	/**
	 * 查询任务类型模板配置列表
	 *
	 * @return
	 */
	@GetMapping("/list")
	public R<?> listDeptTaskTypeTemplate(Page<WmDeptTaskTypeTemplate> page, @RequestParam(name = "deptId") Integer deptId) {
		IPage<WmDeptTaskTypeTemplate> resultPage = wmDeptTaskTypeTemplateService.page(page, Wrappers.<WmDeptTaskTypeTemplate>lambdaQuery()
				.eq(WmDeptTaskTypeTemplate::getDeptId, deptId));
		if (CollUtil.isNotEmpty(resultPage.getRecords())) {
			resultPage.getRecords().forEach(wmDeptTaskTypeTemplate -> {
				WmTaskType taskType = wmTaskTypeService.getById(wmDeptTaskTypeTemplate.getTaskTypeId());
				if (taskType != null) {
					wmDeptTaskTypeTemplate.setTaskTypeName(taskType.getTaskTypeName());
					wmDeptTaskTypeTemplate.setScore(taskType.getScore());
				}
			});
		}
		return R.ok(resultPage);
	}

	/**
	 * 根据id查询任务类型模板配置
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<WmDeptTaskTypeTemplate> getDeptTaskTypeTemplateById(@PathVariable("id") Integer id) {
		WmDeptTaskTypeTemplate taskTypeTemplate = wmDeptTaskTypeTemplateService.getById(id);
		if (taskTypeTemplate != null) {
			WmTaskType taskType = wmTaskTypeService.getById(taskTypeTemplate.getTaskTypeId());
			if (taskType != null) {
				taskTypeTemplate.setTaskTypeName(taskType.getTaskTypeName());
				taskTypeTemplate.setScore(taskType.getScore());
			}
		}
		return R.ok(taskTypeTemplate);
	}

	/**
	 * 保存
	 *
	 * @param wmDeptTaskTypeTemplate
	 * @return
	 */
	@PostMapping
	public R<?> saveDeptTaskTypeTemplate(@RequestBody @Validated({BaseEntity.Create.class}) WmDeptTaskTypeTemplate wmDeptTaskTypeTemplate) {
		// 校验该企业下是否已存在该任务类型
		int existCount = wmDeptTaskTypeTemplateService.count(Wrappers.<WmDeptTaskTypeTemplate>lambdaQuery()
				.eq(WmDeptTaskTypeTemplate::getDeptId, wmDeptTaskTypeTemplate.getDeptId())
				.eq(WmDeptTaskTypeTemplate::getTaskTypeId, wmDeptTaskTypeTemplate.getTaskTypeId()));
		if (existCount > 0) {
			return R.failed("任务类型已存在");
		}
		wmDeptTaskTypeTemplate.setCreateTime(LocalDateTime.now());
		wmDeptTaskTypeTemplate.setCreateUser(SecurityUtils.getUser().getId());
		wmDeptTaskTypeTemplate.setUpdateUser(null);
		wmDeptTaskTypeTemplate.setUpdateTime(null);
		wmDeptTaskTypeTemplateService.save(wmDeptTaskTypeTemplate);

		return R.ok();
	}

	/**
	 * 更新
	 *
	 * @param wmDeptTaskTypeTemplate
	 * @return
	 */
	@PutMapping
	public R<?> updateDeptTaskTypeTemplate(@RequestBody @Validated(BaseEntity.Update.class) WmDeptTaskTypeTemplate wmDeptTaskTypeTemplate) {
		WmDeptTaskTypeTemplate update = new WmDeptTaskTypeTemplate();
		update.setId(wmDeptTaskTypeTemplate.getId());
		update.setTemplate(wmDeptTaskTypeTemplate.getTemplate());
		update.setUpdateTime(LocalDateTime.now());
		update.setUpdateUser(SecurityUtils.getUser().getId());
		wmDeptTaskTypeTemplateService.updateById(update);

		return R.ok();
	}

	/**
	 * 根据id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	public R<?> deleteDeptTaskTypeTemplate(@PathVariable("id") Integer id) {
		wmDeptTaskTypeTemplateService.removeById(id);

		return R.ok();
	}
}
