package com.dxhy.admin.sys.controller;

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

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.admin.sys.entity.SysBusinessEntity;
import com.dxhy.admin.sys.service.SysBusinessService;
import com.dxhy.admin.sys.service.SysUserBusinessService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;
import com.dxhy.common.vo.UserInfo;

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

/**
 * 业务单元管理
 *
 * @author jiaohongyang
 */
@RestController
@RequestMapping("/sys/business")
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class SysBusinessController extends AbstractController {
    @Resource
    private SysBusinessService sysBusinessService;
    @Resource
    private SysUserBusinessService sysUserBusinessService;

    /**
     * 业务单元列表
     */
    @RequestMapping("/list")
    @SysLog("业务单元查询")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        UserInfo userInfo = getUserInfo();
        if (Long.parseLong(userInfo.getId()) != Constant.SUPER_ADMIN && !userInfo.getCompany().contains("ZXQY")) {
            params.put("company", userInfo.getCompany());
        }
        PageUtils page = sysBusinessService.queryPage(params);

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

    /**
     * 业务单元列表
     */
    @RequestMapping("/select")
    public ResponseEntity<?> select() {
        List<SysBusinessEntity> list = sysBusinessService.queryBusinesslist();

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

    /**
     * 业务单元信息
     */
    @RequestMapping("/info/{businesseId}")
    public ResponseEntity<?> info(@PathVariable("businessId") Long businessId) {
        SysBusinessEntity role = sysBusinessService.queryBusinessById(businessId);

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

    /**
     * 保存业务单元
     */
    @RequestMapping("/save")
    @SysLog("进项平台保存业务单元信息")
    public ResponseEntity<?> save(@RequestBody SysBusinessEntity business) {
        try {
            ValidatorUtils.validateEntity(business);
            SysBusinessEntity queryBusiness =
                sysBusinessService.queryBusiness(business.getBusinessCode(), business.getCompany(), null);
            if (queryBusiness != null) {
                return ResponseEntity.ok(R.error("保存失败，业务单元编码重复！"));
            }
            business.setCreateTime(DateUtil.date());
            business.setUpdateTime(DateUtil.date());
            sysBusinessService.saveBusiness(business);

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

        }
    }

    /**
     * 修改业务单元
     */
    @RequestMapping("/update")
    @SysLog("进项平台修改业务单元信息")

    public ResponseEntity<?> update(@RequestBody SysBusinessEntity business) {
        try {
            ValidatorUtils.validateEntity(business);
            SysBusinessEntity queryBusiness =
                sysBusinessService.queryBusiness(business.getBusinessCode(), business.getCompany(), business.getId());
            if (queryBusiness != null) {
                return ResponseEntity.ok(R.error("保存失败，业务单元编码重复！"));
            }
            business.setUpdateTime(DateUtil.date());
            sysBusinessService.update(business);

            // 修改用户与业务单元信息

            sysUserBusinessService.updateBusinessId(business.getBusinessCode(), business.getBusinessName(),
                business.getId());
            return ResponseEntity.ok(R.ok("保存成功"));

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

        }
    }

    /**
     * 删除业务单元
     */
    @RequestMapping("/delete")
    @SysLog("进项平台删除业务单元信息")

    public ResponseEntity<?> delete(long businessId) {
        try {
            sysBusinessService.deleteBatch(businessId);
            // 删除用户与业务单元关系

            return ResponseEntity.ok(R.ok("删除成功"));

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

        }
    }

    /**
     * 根据部门id 查询业务单元
     */
    @RequestMapping("/listInfo")
    public ResponseEntity<?> listInfo(String company) {
        return ResponseEntity.ok(R.ok().put("businessList", sysBusinessService.queryBusinessDeptId(company)));
    }
}
