package com.dxhy.admin.sys.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.dxhy.admin.sys.entity.SysRulesDeptEntity;
import com.dxhy.admin.sys.entity.SysSignRulesEntity;
import com.dxhy.admin.sys.service.SysRulesDeptService;
import com.dxhy.admin.sys.service.SysSignRulesService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 签收校验规则管理
 *
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/sys/rules")
@Slf4j
public class SysSignRulesController extends AbstractController {
    @Resource
    private SysSignRulesService sysSignRulesService;
    @Resource
    private SysRulesDeptService sysRulesDeptService;

    /**
     * 签收规则列表
     */
    @RequestMapping("/list")
    @SysLog("签收规则查询")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {

        PageUtils page = sysSignRulesService.queryPage(params);

        return ResponseEntity.ok(Objects.requireNonNull(R.ok().put("page", page)));
    }

    /**
     * 未分配签收规则列表
     */
    @RequestMapping("/unAllotList")
    @SysLog("签收规则查询")
    public ResponseEntity<?> unAllotList(@RequestBody Map<String, Object> params) {

        return ResponseEntity.ok(Objects.requireNonNull(R.ok().put("list", sysSignRulesService.unAllotList(params))));
    }

    /**
     * 保存签收规则
     */
    @RequestMapping("/save")
    @SysLog("签收规则保存")
    public ResponseEntity<?> save(@RequestBody SysSignRulesEntity rules) {
        try {
            ValidatorUtils.validateEntity(rules);
            SysSignRulesEntity signRulesEntity = sysSignRulesService.queryRules(rules.getRuleCode());
            if (signRulesEntity != null) {
                return ResponseEntity.ok(R.error("保存失败，签收规则编码重复！"));
            }
            rules.setCreateDate(DateUtil.date());
            sysSignRulesService.saveRules(rules);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 分配签收规则
     */
    @RequestMapping("/update")
    @SysLog("签收规则分配")
    public ResponseEntity<?> update(@RequestBody SysSignRulesEntity rules) {
        try {
            ValidatorUtils.validateEntity(rules);
            sysRulesDeptService.saveRulesDept(rules.getRulesDeptList());
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 删除签收规则
     */
    @RequestMapping("/delete")
    @ResponseBody
    @SysLog("签收规则删除")
    public ResponseEntity<?> delete(Long ruleId, Long deptId, String deptType) {
        try {

            if ("0".equals(deptType)) {

                sysSignRulesService.deleteRules(ruleId);
                // 删除机构与签收规则关系
                sysRulesDeptService.deleteBatchRules(ruleId, deptId);
            } else {
                // 删除机构与签收规则关系
                sysRulesDeptService.deleteBatchRules(ruleId, deptId);
            }
            return ResponseEntity.ok(R.ok("删除成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.ok("删除失败"));

        }
    }

    @RequestMapping("/info/{ruleId}")
    @ResponseBody
    public ResponseEntity<?> info(@PathVariable("ruleId") Long ruleId) {
        SysSignRulesEntity rule = sysSignRulesService.getById(ruleId);

        // 获取用户关联 权限
        List<SysRulesDeptEntity> rulesDeptEntities = sysRulesDeptService.getRuleDept(ruleId);
        if (rulesDeptEntities != null && rulesDeptEntities.size() > 0) {
            rule.setRulesDeptList(rulesDeptEntities);
        }

        return ResponseEntity.ok(R.ok().put("rule", rule));
    }
}
