/*
 *    Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: hnqz
 */

package com.qunzhixinxi.hnqz.admin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.entity.WmDaAgent;
import com.qunzhixinxi.hnqz.admin.entity.WmDaDrugEnt;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptMapper;
import com.qunzhixinxi.hnqz.admin.service.*;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskTypeEntRef;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.sql.rowset.serial.SerialArray;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 药企给代理商设定积分规则
 *
 * @author gaoyang
 * @date 2020-07-02 23:35:21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmtasktypeentref")
@Api(value = "wmtasktypeentref", tags = "药企给代理商设定积分规则管理")
public class WmTaskTypeEntRefController {

	private final WmTaskTypeEntRefService wmTaskTypeEntRefService;
	private final WmTaskTypeService wmTaskTypeService;
	private final WmDaAgentService wmDaAgentService;
	private final SysDeptService sysDeptService;
	private final WmDaDrugEntService wmDaDrugEntService;
	private final SysDeptMapper sysDeptMapper;

	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param wmTaskTypeEntRef 药企给代理商设定积分规则
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_view')" )
	public R getWmTaskTypeEntRefPage(Page page, WmTaskTypeEntRef wmTaskTypeEntRef) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept deptId = sysDeptMapper.selectDeptId(sysDept1);
		SysDept deptId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		WmDaDrugEnt drId = wmDaDrugEntService.selectByDeptId(deptId.getDeptId());
		wmTaskTypeEntRef.setDrugentId((String.valueOf(drId.getId())));
		return R.ok(wmTaskTypeEntRefService.page(page, Wrappers.query(wmTaskTypeEntRef)));
	}

	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param wmTaskTypeEntRef 药企给代理商设定积分规则
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/pages")
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_view')" )
	public R getWmTaskTypeEntRefPages(Page page, WmTaskTypeEntRef wmTaskTypeEntRef) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept deptId = sysDeptMapper.selectDeptId(sysDept1);

		SysDept deptId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		WmDaAgent anId = wmDaAgentService.selectByDeptId(deptId.getDeptId());
		wmTaskTypeEntRef.setAgentId(String.valueOf(anId.getId()));
		wmTaskTypeEntRef.setDrugentId("");
		return R.ok(wmTaskTypeEntRefService.page(page, Wrappers.query(wmTaskTypeEntRef)));
	}


	/**
	 * 通过id查询药企给代理商设定积分规则
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_view')" )
	public R getById(@PathVariable("id") Integer id) {
		return R.ok(wmTaskTypeEntRefService.getById(id));
	}

	/**
	 * 新增药企给代理商设定积分规则
	 *
	 * @param wmTaskTypeEntRef 药企给代理商设定积分规则
	 * @return R
	 */
	@ApiOperation(value = "新增药企给代理商设定积分规则", notes = "新增药企给代理商设定积分规则")
	@SysLog("新增药企给代理商设定积分规则")
	@PostMapping
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_add')" )
	public R save(@RequestBody WmTaskTypeEntRef wmTaskTypeEntRef) {
		wmTaskTypeEntRef.setDrugentId(SecurityUtils.getUser().getDrugEntId());

		WmTaskType wmTaskType = new WmTaskType();
		wmTaskType.setDurgentId(SecurityUtils.getUser().getDrugEntId());
		wmTaskType.setLevel1Id(wmTaskTypeEntRef.getAgentId());
		wmTaskType.setDelFlag("0");
		List<WmTaskType> list = wmTaskTypeService.list(Wrappers.query(wmTaskType));
		WmDaAgent wmDaAgent = wmDaAgentService.getById(wmTaskTypeEntRef.getAgentId());
		if (list.size() > 0) {
			return R.failed("一代积分规则已分配");
		} else {
			if (null != wmDaAgent) {
				wmTaskTypeEntRef.setDeptId(wmDaAgent.getDeptId());
			}
			boolean isSave = wmTaskTypeEntRefService.save(wmTaskTypeEntRef);

			WmTaskType wmTaskTypeQ = new WmTaskType();
			wmTaskTypeQ.setTaskTypeLevel("1");
			wmTaskTypeQ.setDelFlag("0");
			wmTaskTypeQ.setStatus("0");
			List<WmTaskType> tempList = wmTaskTypeService.list(Wrappers.query(wmTaskTypeQ));
			List<WmTaskType> newList = new ArrayList<>();

			for (WmTaskType wmTaskType1 : tempList) {
				WmTaskType newOne = wmTaskType1;
				newOne.setId(null);
				newOne.setLevel1Id(wmTaskTypeEntRef.getAgentId());
				newOne.setDurgentId(SecurityUtils.getUser().getDrugEntId());
				newOne.setTaskTypeLevel("2");
				newOne.setTaskEntId(wmTaskTypeEntRef.getAgentId());
				newOne.setDeptId(wmDaAgent.getDeptId() + "");
				newOne.setCreateTime(LocalDateTime.now());
				newOne.setStatus("0");
				newOne.setBaseId(wmTaskType1.getId());
				newList.add(newOne);
			}
			wmTaskTypeService.saveBatch(newList);

			return R.ok(isSave);

		}
	}


	/**
	 * 新增药企给代理商设定积分规则
	 *
	 * @param wmTaskTypeEntRef 药企给代理商设定积分规则
	 * @return R
	 */
	@ApiOperation(value = "新增药企给代理商设定积分规则", notes = "新增药企给代理商设定积分规则")
	@SysLog("新增药企给代理商设定积分规则")
	@PostMapping("saves")
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_add')" )
	public R saves(@RequestBody WmTaskTypeEntRef wmTaskTypeEntRef) {

//		SysDept sysDept1 = new SysDept();
//		sysDept1.setDeptId(SecurityUtils.getUser().getDeptId());
//		SysDept deptId = sysDeptMapper.selectDeptId(sysDept1);

		SysDept deptId = sysDeptService.getById(SecurityUtils.getUser().getDeptId());
		WmDaDrugEnt wmDaDrugEnt = wmDaDrugEntService.selectByDeptId(deptId.getDeptId());

		wmTaskTypeEntRef.setDrugentId(wmDaDrugEnt.getId());

		WmTaskType wmTaskType = new WmTaskType();
		wmTaskType.setDurgentId(wmTaskTypeEntRef.getDrugentId());
		wmTaskType.setLevel1Id(wmTaskTypeEntRef.getAgentId());
		wmTaskType.setDelFlag("0");
		List<WmTaskType> list = wmTaskTypeService.list(Wrappers.query(wmTaskType));
		WmDaAgent wmDaAgent = wmDaAgentService.getById(wmTaskTypeEntRef.getAgentId());
		WmDaDrugEnt wmDaDrugEnt1 = wmDaDrugEntService.getById(wmTaskTypeEntRef.getDrugentId());
//		SysDept sysDept2 = new SysDept();
//		sysDept2.setDeptId(Integer.valueOf(wmDaDrugEnt1.getDeptId()));
//		SysDept DrugEntDeptId = sysDeptMapper.selectDeptId(sysDept2);

		SysDept DrugEntDeptId = sysDeptService.getById(Integer.valueOf(wmDaDrugEnt1.getDeptId()));

		if (list.size() > 0) {
//			SysDept sysDept3 = new SysDept();
//			sysDept3.setDeptId(Integer.valueOf(wmDaAgent.getDeptId()));
//			SysDept AgentDeptId = sysDeptMapper.selectDeptId(sysDept3);

			SysDept AgentDeptId = sysDeptService.getById(Integer.valueOf(wmDaAgent.getDeptId()));
//			SysDept AgentDeptId = sysDeptService.selectByName(wmDaAgent.getGsmc());
			if (AgentDeptId.getLevel() == 3) {
				for (WmTaskType wmTaskType1 : list) {
					if (wmTaskType1.getLevel1Id().equals(wmTaskTypeEntRef.getAgentId()) &&
							wmTaskType1.getDurgentId().equals(wmTaskTypeEntRef.getDrugentId())) {
					}
				}
				return R.failed("药企已分配一代积分规则");
			}
		}
		else if (null != wmDaAgent) {
			wmTaskTypeEntRef.setDeptId(wmDaAgent.getDeptId());
		}else if (DrugEntDeptId.getLevel() == 2) {
			return R.failed("药企只能分配积分规则");
		}

		boolean isSave = wmTaskTypeEntRefService.save(wmTaskTypeEntRef);
		WmTaskType wmTaskTypeQ = new WmTaskType();
		wmTaskTypeQ.setTaskTypeLevel("1");
		wmTaskTypeQ.setDelFlag("0");
		wmTaskTypeQ.setStatus("0");
		List<WmTaskType> tempList = wmTaskTypeService.list(Wrappers.query(wmTaskTypeQ));
		List<WmTaskType> newList = new ArrayList<>();

		for (WmTaskType wmTaskType1 : tempList) {
			WmTaskType newOne = wmTaskType1;
			newOne.setId(null);
			newOne.setLevel1Id(wmTaskTypeEntRef.getAgentId());
			newOne.setDurgentId(wmTaskTypeEntRef.getDrugentId());
			newOne.setTaskTypeLevel("2");
			newOne.setTaskEntId(wmTaskTypeEntRef.getAgentId());
			newOne.setDeptId(wmDaAgent.getDeptId() + "");
			newOne.setCreateTime(LocalDateTime.now());
			newOne.setStatus("0");
			newOne.setBaseId(wmTaskType1.getId());
			newOne.setRuleId(wmTaskType1.getDurgentId() + wmTaskType1.getLevel1Id());
			newOne.setParentId(wmTaskType1.getParentId());
			newList.add(newOne);
		}


		wmTaskTypeService.saveBatch(newList);

		return R.ok(isSave);
	}


	/**
	 * 修改药企给代理商设定积分规则
	 *
	 * @param wmTaskTypeEntRef 药企给代理商设定积分规则
	 * @return R
	 */
	@ApiOperation(value = "修改药企给代理商设定积分规则", notes = "修改药企给代理商设定积分规则")
	@SysLog("修改药企给代理商设定积分规则")
	@PutMapping
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_edit')" )
	public R updateById(@RequestBody WmTaskTypeEntRef wmTaskTypeEntRef) {
		return R.ok(wmTaskTypeEntRefService.updateById(wmTaskTypeEntRef));
	}

	/**
	 * 通过id删除药企给代理商设定积分规则
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除药企给代理商设定积分规则", notes = "通过id删除药企给代理商设定积分规则")
	@SysLog("通过id删除药企给代理商设定积分规则")
	@DeleteMapping("/{id}")
	//@PreAuthorize("@pms.hasPermission('admin_wmtasktypeentref_del')" )
	public R removeById(@PathVariable Integer id) {
		return R.ok(wmTaskTypeEntRefService.removeById(id));
	}


	/**
	 * 获取药企列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R listWmScorePackage(WmTaskTypeEntRef wmTaskTypeEntRef) {
		wmTaskTypeEntRef.setDelFlag("0");
		wmTaskTypeEntRef.setEnableFlag("0");
		wmTaskTypeEntRef.setDrugentId(SecurityUtils.getUser().getDrugEntId());
		return R.ok(wmTaskTypeEntRefService.list(Wrappers.query(wmTaskTypeEntRef)));
	}

}
