package com.dcsoft.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.SysRule;
import com.dcsoft.system.service.ISysConfigService;
import com.dcsoft.system.service.ISysPeopleService;
import com.dcsoft.system.service.ISysRuleService;
import com.dcsoft.system.uniubi.service.ISysApiService;
import com.dcsoft.system.uniubi.service.ISysSdkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.system.domain.SysManageRecord;
import com.dcsoft.system.service.ISysManageRecordService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;

/**
 * 授权管理记录Controller
 *
 * @author dcsoft
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/manageRecord")
public class SysManageRecordController extends BaseController
{
    @Autowired
    private ISysManageRecordService sysManageRecordService;

    @Autowired
    private ISysRuleService ruleService;

    @Autowired
    private ISysPeopleService peopleService;

    @Autowired
    private ISysApiService sysApiService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected ISysSdkService sdkService;

    /**
     * 查询授权管理记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysManageRecord sysManageRecord)
    {
        startPage();
        List<SysManageRecord> list = sysManageRecordService.selectSysManageRecordList(sysManageRecord);
        return getDataTable(list);
    }

    /**
     * 导出授权管理记录列表
     */
    @Log(title = "授权管理记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysManageRecord sysManageRecord)
    {
        List<SysManageRecord> list = sysManageRecordService.selectSysManageRecordList(sysManageRecord);
        ExcelUtil<SysManageRecord> util = new ExcelUtil<SysManageRecord>(SysManageRecord.class);
        util.exportExcel(response, list, "授权管理记录数据");
    }

    /**
     * 获取授权管理记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysManageRecordService.selectSysManageRecordById(id));
    }

    /**
     * 新增授权管理记录
     */
    @Log(title = "授权管理记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysManageRecord manageRecord)
    {
        Boolean flag=false;
        String cloud = configService.selectConfigByKey("sys.equipment.cloud");
        /*if("true".equals(cloud)){
                if(manageRecord.getType()==2){//人员授权管理
                    if(StringUtils.isNotEmpty(manageRecord.getPeopleIds()+"")){
                        for(String peopleId:manageRecord.getPeopleIds()){
                            flag=true;
                            manageRecord.setPeopleId(Long.parseLong(peopleId));
                            SysRule rule=ruleService.selectSysRuleById(manageRecord.getRuleId());
                            SysPeople people =peopleService.selectSysPeopleById(Long.parseLong(peopleId));
                            //添加人员授权信息
                            String data=sysApiService.authDevice(rule,people);
                            if("".equals(data)){
                                return AjaxResult.error("该规则未绑定设备!");
                            }
                            sysManageRecordService.insertSysManageRecord(manageRecord);
                        }
                    }
                }
                if(manageRecord.getType()==1) {//部门授权管理
                    if (StringUtils.isNotEmpty(manageRecord.getBranchIds() + "")) {
                        for (String branchId : manageRecord.getBranchIds()) {
                            flag = true;
                            manageRecord.setBranchId(Long.parseLong(branchId));
                            sysManageRecordService.insertSysManageRecord(manageRecord);
                            //添加部门人员授权信息
                            SysRule rule=ruleService.selectSysRuleById(manageRecord.getRuleId());
                            SysPeople peoples=new SysPeople();
                            peoples.setBranchId(branchId);
                            List<SysPeople> peopleList=peopleService.selectSysPeopleList(peoples);
                            for(SysPeople people:peopleList){
                                String data=sysApiService.authDevice(rule,people);
                                if("".equals(data)){
                                    return AjaxResult.error("该规则未绑定设备!");
                                }
                                manageRecord.setPeopleId(people.getId());
                                sysManageRecordService.insertSysManageRecord(manageRecord);
                            }
                        }
                    }
                }
        }else{
            if(manageRecord.getType()==2){//人员授权管理
                if(StringUtils.isNotEmpty(manageRecord.getPeopleIds()+"")) {
                    for(String peopleId:manageRecord.getPeopleIds()){
                        flag=true;
                        manageRecord.setPeopleId(Long.parseLong(peopleId));
                        SysRule rule=ruleService.selectSysRuleById(manageRecord.getRuleId());
                        SysPeople people =peopleService.selectSysPeopleById(Long.parseLong(peopleId));
                        //添加人员授权信息
                        String data=sdkService.authDevice(rule,people);
                        if("".equals(data)){
                            return AjaxResult.error("该规则未绑定设备!");
                        }
                        sysManageRecordService.insertSysManageRecord(manageRecord);
                    }
                }
            }
            if(manageRecord.getType()==1) {//部门授权管理
                if (StringUtils.isNotEmpty(manageRecord.getBranchIds() + "")) {
                    for (String branchId : manageRecord.getBranchIds()) {
                        flag = true;
                        manageRecord.setBranchId(Long.parseLong(branchId));
                        sysManageRecordService.insertSysManageRecord(manageRecord);
                        //添加部门人员授权信息
                        SysRule rule=ruleService.selectSysRuleById(manageRecord.getRuleId());
                        SysPeople peoples=new SysPeople();
                        peoples.setBranchId(branchId);
                        List<SysPeople> peopleList=peopleService.selectSysPeopleList(peoples);
                        for(SysPeople people:peopleList){
                            String data=sdkService.authDevice(rule,people);
                            if("".equals(data)){
                                return AjaxResult.error("该规则未绑定设备!");
                            }
                            manageRecord.setPeopleId(people.getId());
                            sysManageRecordService.insertSysManageRecord(manageRecord);
                        }
                    }
                }
            }
        }*/
        return flag ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改授权管理记录
     */
    @Log(title = "授权管理记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysManageRecord sysManageRecord)
    {
        return toAjax(sysManageRecordService.updateSysManageRecord(sysManageRecord));
    }

    /**
     * 删除授权管理记录
     */
    @Log(title = "授权管理记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysManageRecordService.deleteSysManageRecordByIds(ids));
    }
}
