/*
 *    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.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskRule;
import com.qunzhixinxi.hnqz.admin.service.WmTaskRuleService;
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 java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 任务积分规则
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmtaskrule" )
@Api(value = "wmtaskrule", tags = "任务积分规则管理")
public class WmTaskRuleController {

    private final  WmTaskRuleService wmTaskRuleService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wmTaskRule 任务积分规则
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
	//@PreAuthorize("@pms.hasPermission('admin_wmtaskrule_view')" )
    public R getWmTaskRulePage(Page page, WmTaskRule wmTaskRule) {
		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

    	if(SecurityUtils.getRoles().contains(3))
		{
			wmTaskRule.setDrugentId(SecurityUtils.getUser().getDrugEntId());
		}
        return R.ok(wmTaskRuleService.page(page, Wrappers.query(wmTaskRule)));
    }


    /**
     * 通过id查询任务积分规则
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
	//@PreAuthorize("@pms.hasPermission('admin_wmtaskrule_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(wmTaskRuleService.getById(id));
    }

    /**
     * 新增任务积分规则
     * @param wmTaskRule 任务积分规则
     * @return R
     */
    @ApiOperation(value = "新增任务积分规则", notes = "新增任务积分规则")
    @SysLog("新增任务积分规则" )
    @PostMapping
	//@PreAuthorize("@pms.hasPermission('admin_wmtaskrule_add')" )
    public R save(@RequestBody WmTaskRule wmTaskRule) {
		wmTaskRule.setDrugentId(SecurityUtils.getUser().getDrugEntId());
		wmTaskRule.setCreateTime(LocalDateTime.now());

		WmTaskRule query=new WmTaskRule();
		query.setRuleMonth(wmTaskRule.getRuleMonth());
		query.setDrugentId(SecurityUtils.getUser().getDrugEntId());
		List<WmTaskRule> list=wmTaskRuleService.list(Wrappers.query(query));

		if(list.size()>0)
		{
			return R.failed("当前月份积分规则已存在");
		}

        return R.ok(wmTaskRuleService.save(wmTaskRule));
    }

    /**
     * 修改任务积分规则
     * @param wmTaskRule 任务积分规则
     * @return R
     */
    @ApiOperation(value = "修改任务积分规则", notes = "修改任务积分规则")
    @SysLog("修改任务积分规则" )
    @PutMapping
	//@PreAuthorize("@pms.hasPermission('admin_wmtaskrule_edit')" )
    public R updateById(@RequestBody WmTaskRule wmTaskRule) {
    	return R.ok(wmTaskRuleService.updateById(wmTaskRule));
    }

    /**
     * 通过id删除任务积分规则
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除任务积分规则", notes = "通过id删除任务积分规则")
    @SysLog("通过id删除任务积分规则" )
    @DeleteMapping("/{id}" )
	//@PreAuthorize("@pms.hasPermission('admin_wmtaskrule_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(wmTaskRuleService.removeById(id));
    }

	/**
	 * 获取药企列表
	 * @return 角色列表
	 */
	@GetMapping("/listZb")
	public R listZb(WmTaskRule wmTaskRule) {
		if(SecurityUtils.getRoles().contains(3))
		{
			wmTaskRule.setDrugentId(SecurityUtils.getUser().getDrugEntId());
		}
		wmTaskRule.setDelFlag("0");
		wmTaskRule.setEnableFlag("0");
		List<WmTaskRule> list=wmTaskRuleService.list(Wrappers.query(wmTaskRule));
		if(list.size()>0)
		{
			List<String> list1=new ArrayList<>();
			for (WmTaskRule wmTaskRule1:list) {
				list1.add(wmTaskRule1.getRuleMonth());
			}

			List<String> list2=list1.stream().distinct().collect(Collectors.toList());

			List<WmTaskRule> listWm=new ArrayList<>();
			for (String str:list2) {
				WmTaskRule wmt=new WmTaskRule();
				wmt.setRuleMonth(str);
				wmt.setId(str);
				listWm.add(wmt);
			}
			return R.ok(listWm);

		}
		return R.ok();
	}


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