package com.zd.laboratory.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.bean.BeanUtils;
import com.zd.common.core.valid.ValidGroup;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabBlacklist;
import com.zd.laboratory.domain.dto.LabBlacklistAdd;
import com.zd.laboratory.domain.dto.LabBlacklistDTO;
import com.zd.laboratory.domain.vo.LabBlackdetailVO;
import com.zd.laboratory.domain.vo.LabBlacklistVO;
import com.zd.laboratory.service.ILabBlackdetailService;
import com.zd.laboratory.service.ILabBlacklistService;
import com.zd.laboratory.service.ILabNegativeListRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 黑名单Controller
 *
 * @author zd
 * @date 2021-09-01
 */
@RestController
@RequestMapping("/blacklist")
public class LabBlacklistController extends BaseController {
    @Autowired
    private ILabBlacklistService labBlacklistService;
    @Autowired
    private ILabBlackdetailService labBlackdetailService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ILabNegativeListRecordsService iLabNegativeListRecordsService;

    /**
     * 查询黑名单列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKLIST + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo<LabBlacklistVO> list(LabBlacklistDTO labBlacklist) {
        startPage();
        List<LabBlacklistVO> list = labBlacklistService.selectLabBlackVOList(labBlacklist);
        return getDataTable(list);
    }

    /**
     * 根据用户查询黑名单列表
     */
    @PostMapping("/info/{userId}")
    public AjaxResult userBlacklist(@PathVariable("userId") Long userId) {
        LabBlacklistDTO labBlacklist = new LabBlacklistDTO();
        labBlacklist.setJoinUserId(userId);
        List<LabBlacklist> list = labBlacklistService.selectLabBlacklistList(labBlacklist);
        return AjaxResult.success(list);
    }

    /**
     * 根据用户查询是否在黑名单
     */
    @GetMapping("/getUserBoolean/{userId}")
    public AjaxResult getUserBoolean(@PathVariable("userId") Long userId) {
        LabBlacklistDTO labBlacklist = new LabBlacklistDTO();
        labBlacklist.setJoinUserId(userId);
        return AjaxResult.success(labBlacklistService.getUserBoolean(labBlacklist));
    }

    /**
     * 新增黑名单（老方法，暂时不用了）
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKLIST + PerFun.ADD)
    @Log(title = "黑名单", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated(ValidGroup.Crud.Create.class) LabBlacklistAdd add) {
        LabBlacklist labBlacklist = new LabBlacklist();
        BeanUtils.copyBeanProp(labBlacklist, add);
        return ResultData.result(labBlacklistService.setBackUser(labBlacklist));
    }

    /**
     * 移除黑名单（老方法暂时不用了）
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKLIST + PerFun.REMOVE)
    @Log(title = "黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public ResultData remove(@PathVariable Long... id) {
        return ResultData.result(labBlacklistService.deleteLabBlacklistByIds(id) > 0, () -> "未找到黑名单记录！");
    }


    /**
     * 查询黑名单详情列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKDETAIL + PerFun.LIST)
    @GetMapping("/getBlackDetail")
    public TableDataInfo<LabBlackdetailVO> getBlackDetail(LabBlackdetailVO labBlackdetailVO) {
        startPage();
        List<LabBlackdetailVO> list = labBlackdetailService.selectLabBlackdetailList(labBlackdetailVO);
        return getDataTable(list);
    }

    /**
     * 查询个人黑名单详情列表
     */
    @GetMapping("/myBlackDetail")
    public TableDataInfo<LabBlackdetailVO> myBlackDetail(LabBlackdetailVO labBlackdetailVO) {
        startPage();
        LabBlacklist labBlacklist = labBlacklistService.selectLabBlacklistById(SecurityUtils.getUserId());
        if(labBlacklist!=null){
            labBlackdetailVO.setBlackId(labBlacklist.getId());
            List<LabBlackdetailVO> list = labBlackdetailService.selectLabBlackdetailList(labBlackdetailVO);
            return getDataTable(list);
        }

        return getDataTable(Collections.emptyList());
    }


    /**
     * 新增黑名单
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKLIST + PerFun.ADD)
    @Log(title = "黑名单", businessType = BusinessType.INSERT)
    @PostMapping("/addBlacklist")
    public ResultData addBlacklist(@RequestBody LabBlacklistVO labBlacklistVO) {
        return ResultData.result(labBlacklistService.addBlacklist(labBlacklistVO));
    }

    /**
     * 黑名单完成学习规则
     */
    @Log(title = "黑名单完成学习规则", businessType = BusinessType.UPDATE)
    @PostMapping("/blacklistLearningRules")
    public ResultData blacklistLearningRules(@RequestBody LabBlackdetailVO labBlackdetailVO) {
        logger.error("学习离开负面清单==============-----------==============++++"+labBlackdetailVO.toString());
        if(labBlackdetailVO.getNegativeOrBlack().equals("2")){
            //黑名单处理
            return ResultData.result(labBlacklistService.blacklistCompletionLearningRules(labBlackdetailVO));
        }else if(labBlackdetailVO.getNegativeOrBlack().equals("1")){
            //负面清单处理
            return ResultData.result(iLabNegativeListRecordsService.negivelistLearningRules(labBlackdetailVO));
        }
        return ResultData.result(false,()->"类型错误，请联系管理员!!");
    }

    /**
     * 移除黑名单
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_BLACKLIST + PerFun.REMOVE)
    @Log(title = "移除黑名单", businessType = BusinessType.UPDATE)
    @PostMapping("/removeBlacklist")
    public ResultData removeBlacklist(@RequestBody LabBlacklist labBlacklist) {
        return ResultData.result(labBlacklistService.removeBlacklist(labBlacklist));
    }

    /**
     * 获取黑名单惩罚时间区间
     */
    @RequestMapping("/getBlackTimeArea")
    public AjaxResult getBlackTimeArea(@RequestBody LabBlackdetailVO labBlackdetailVO) {
        if(labBlackdetailVO.getNegativeOrBlack().equals("2")){
            //黑名单处理
            return AjaxResult.success(labBlacklistService.getBlackTimeArea(labBlackdetailVO));
        }else if(labBlackdetailVO.getNegativeOrBlack().equals("1")){
            //负面清单处理
            return AjaxResult.success(iLabNegativeListRecordsService.getNegiveTimeArea(labBlackdetailVO));
        }
        return AjaxResult.success("类型错误，请联系管理员!!");

    }

}
