package com.yijia.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.yijia.common.core.domain.entity.SysUser;
import com.yijia.system.service.ISysUserService;
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.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.system.domain.SysAgent;
import com.yijia.system.service.ISysAgentService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 代理机构Controller
 *
 * @author yijia
 * @date 2021-07-01
 */
@RestController
@RequestMapping("/system/agent")
public class SysAgentController extends BaseController
{
    @Autowired
    private ISysAgentService sysAgentService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询代理机构列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAgent sysAgent)
    {
        startPage();
        List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        return getDataTable(list);
    }

    /**
     * 导出代理机构列表
     */
    @Log(title = "代理机构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAgent sysAgent)
    {
        List<SysAgent> list = sysAgentService.selectSysAgentList(sysAgent);
        ExcelUtil<SysAgent> util = new ExcelUtil<SysAgent>(SysAgent.class);
        return util.exportExcel(list, "agent");
    }

    /**
     * 获取代理机构详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(sysAgentService.selectSysAgentById(id));
    }

    /**
     * 新增代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.INSERT)
    @PostMapping("/addSysAgent")
    public AjaxResult addSysAgent(@RequestBody SysAgent sysAgent)
    {
        List<Integer> sysAgentlist =new ArrayList<>();
        try {
            if(sysAgent!=null && sysAgent.getFlag()!=null && sysAgent.getFlag().equals("1")){
                if(sysAgent.getList()!=null &&sysAgent.getList().size()>0 && sysAgent.getUserId()!=null){
                    SysUser user = sysUserService.selectUserById(sysAgent.getUserId());
                    if(user.getAgentFlag()!=null &&user.getAgentFlag().equals("1")){
                        for(String group : sysAgent.getList()){
                            SysAgent agent = new SysAgent();
                            agent.setGroupId(Long.valueOf(group));
                            agent.setUserId(sysAgent.getUserId());
                            sysAgentService.insertSysAgent(agent);
                        }
                    }
                }
            }else if(sysAgent!=null && sysAgent.getFlag()!=null && sysAgent.getFlag().equals("0")){
                if(sysAgent.getList()!=null &&sysAgent.getList().size()>0 && sysAgent.getUserId()!=null){
                    for(String group : sysAgent.getList()){
                        SysAgent agent = new SysAgent();
                        agent.setGroupId(Long.valueOf(group));
                        agent.setUserId(sysAgent.getUserId());
                        SysAgent sysAgent1 =  sysAgentService.selectSysAgent(agent);
                        if (sysAgent1!=null) {
                             sysAgentService.deleteSysAgentById(sysAgent1.getId().intValue());
                        }
                    }
                }
            }
           sysAgentlist = sysAgentService.sysAgentlist(sysAgent);
        }catch (Exception e){
            System.out.println("------------------------------------"+e.getLocalizedMessage()+"");
            e.printStackTrace();
        }
        return AjaxResult.success(sysAgentlist);
    }

    /**
     * 查询代理已存在的
     */
    @GetMapping("/sysAgentlist")
    public AjaxResult sysAgentlist(SysAgent sysAgent)
    {
        return  AjaxResult.success(sysAgentService.sysAgentlist(sysAgent));
    }


    /**
     * 修改代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAgent sysAgent)
    {
        return toAjax(sysAgentService.updateSysAgent(sysAgent));
    }

    /**
     * 删除代理机构
     */
    @Log(title = "代理机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysAgentService.deleteSysAgentByIds(ids));
    }
}
