package com.dcsoft.system.controller;

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

import com.alibaba.fastjson.JSONObject;
import com.dcsoft.system.domain.SysSyncItem;
import com.dcsoft.system.utils.HttpUtil;
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.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.domain.SysSyncRule;
import com.dcsoft.system.service.ISysSyncRuleService;
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 nichun
 * @date 2024-01-11
 */
@RestController
@RequestMapping("/syncRule")
public class SysSyncRuleController extends BaseController
{
    @Autowired
    private ISysSyncRuleService sysSyncRuleService;

    /**
     * 查询同步规则列表
     */
    @RequiresPermissions("system:syncRule:list")
    @GetMapping("/list")
    public TableDataInfo list(SysSyncRule sysSyncRule)
    {
        startPage();
        List<SysSyncRule> list = sysSyncRuleService.selectSysSyncRuleList(sysSyncRule);
        return getDataTable(list);
    }

    /**
     * 导出同步规则列表
     */
    @RequiresPermissions("system:syncRule:export")
    @Log(title = "同步规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSyncRule sysSyncRule)
    {
        List<SysSyncRule> list = sysSyncRuleService.selectSysSyncRuleList(sysSyncRule);
        ExcelUtil<SysSyncRule> util = new ExcelUtil<SysSyncRule>(SysSyncRule.class);
        util.exportExcel(response, list, "同步规则数据");
    }

    /**
     * 获取同步规则详细信息
     */
    @RequiresPermissions("system:syncRule:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysSyncRule rule=sysSyncRuleService.selectSysSyncRuleById(id);
        List<SysSyncItem> list= sysSyncRuleService.selectSysSyncRuleItem(id);
        if(list.size()>0){
            String[] branchIds=new String[list.size()];
            for(int i=0;i<list.size();i++){
                branchIds[i]=list.get(i).getDept()+"";
            }
            rule.setBranchIds(branchIds);
        }
        return success(rule);
    }

    /**
     * 新增同步规则
     */
    @RequiresPermissions("system:syncRule:add")
    @Log(title = "同步规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSyncRule sysSyncRule)
    {
        int i=sysSyncRuleService.insertSysSyncRule(sysSyncRule);
        if(sysSyncRule.getBranchIds()!=null){
            for(String branchId:sysSyncRule.getBranchIds()){
                SysSyncItem item=new SysSyncItem();
                item.setRuleId(sysSyncRule.getId());
                item.setDept(branchId);
                sysSyncRuleService.insetSysSyncRuleItem(item);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改同步规则
     */
    @RequiresPermissions("system:syncRule:edit")
    @Log(title = "同步规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSyncRule sysSyncRule)
    {
        sysSyncRuleService.deleteSysSyncRuleItem(sysSyncRule.getId());
        if(sysSyncRule.getBranchIds()!=null){
            for(String branchId:sysSyncRule.getBranchIds()){
                SysSyncItem item=new SysSyncItem();
                item.setRuleId(sysSyncRule.getId());
                item.setDept(branchId);
                sysSyncRuleService.insetSysSyncRuleItem(item);
            }
        }
        return toAjax(sysSyncRuleService.updateSysSyncRule(sysSyncRule));
    }

    /**
     * 删除同步规则
     */
    @RequiresPermissions("system:syncRule:remove")
    @Log(title = "同步规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            sysSyncRuleService.deleteSysSyncRuleItem(id);
        }
        return toAjax(sysSyncRuleService.deleteSysSyncRuleByIds(ids));
    }



    @PostMapping(value = "/checkUrl")
    public AjaxResult checkUrl(@RequestBody String url)
    {
        JSONObject object=new JSONObject();
        String data=HttpUtil.postDataJson(url,object);
        return success(data);
    }
}
