package com.ruoyi.web.controller.back;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.voluntary.domain.BusiAgents;
import com.ruoyi.voluntary.domain.SysLocations;
import com.ruoyi.voluntary.dto.SchoolDto;
import com.ruoyi.voluntary.service.IBusiAgentsService;
import com.ruoyi.voluntary.service.ISysLocationsService;
import org.springframework.security.access.prepost.PreAuthorize;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.voluntary.domain.BusiSchools;
import com.ruoyi.voluntary.service.IBusiSchoolsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校Controller
 *
 * @author voluntary
 * @date 2022-11-20
 */
@RestController
@RequestMapping("/voluntary/busiSchools")
public class BackBusiSchoolsController extends BaseController
{
    @Autowired
    private IBusiSchoolsService busiSchoolsService;
    @Autowired
    private ISysLocationsService locationsService;
    @Autowired
    private IBusiAgentsService agentsService;

    /**
     * 查询学校列表
     */
//    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiSchools busiSchools)
    {
        startPage();
        List<BusiSchools> list = busiSchoolsService.selectBusiSchoolsList(busiSchools);
        Map<Long,BusiAgents> map = agentsService.selectBusiAgentsList(new BusiAgents())
                .stream().collect(Collectors.toMap(BusiAgents::getAgentId, Function.identity()));

        list.forEach(ll->{
            String location = locationsService.getProCityArea(ll);
            ll.setLocation(location);
            if(map.containsKey(ll.getAgentId())){
                ll.setAgentName(map.get(ll.getAgentId()).getName());
            }
        });
        return getDataTable(list);
    }

    @GetMapping("/list2")
    public TableDataInfo list2(BusiSchools busiSchools)
    {
        startPage();
//        busiSchools.setAgentId(UserRoleUtils.getBackBusinessUserCode());
        List<BusiSchools> list = busiSchoolsService.selectBusiSchoolsList(busiSchools);
        //去掉重复代理商
        Map<Long,BusiSchools> map = list.stream().collect(Collectors.toMap(BusiSchools::getAgentId, Function.identity()));

        list.forEach(ll->{
            map.put(ll.getAgentId(),ll);
        });
        List<BusiSchools> list2=new ArrayList<>();
        map.forEach((k,v)->{
            list2.add(v);
        });

        list2.forEach(ll->{
            String location = locationsService.getProCityArea(ll);
            ll.setLocation(location);
        });
        return getDataTable(list2);
    }

    @GetMapping("/getAllBusiSchools")
    public AjaxResult getAllBusiSchools(BusiSchools busiSchools)
    {
        List<BusiSchools> list = busiSchoolsService.selectBusiSchoolsList(busiSchools);
        return AjaxResult.success(list);
    }

    /**
     * 根据代理商查找学校
     * @param busiSchools
     * @return
     */
    @GetMapping("/getAreaSchoolsByAgentId")
    public TableDataInfo getAreaSchoolsByAgentCode(BusiSchools busiSchools)
    {
        List<SchoolDto> list = busiSchoolsService.getAreaSchoolsByAgentId(busiSchools.getAgentId());
        return getDataTable(list);
    }

    /**
     * 导出学校列表
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:export')")
    @Log(title = "学校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiSchools busiSchools)
    {
        List<BusiSchools> list = busiSchoolsService.selectBusiSchoolsList(busiSchools);
        ExcelUtil<BusiSchools> util = new ExcelUtil<BusiSchools>(BusiSchools.class);
        util.exportExcel(response, list, "学校数据");
    }

    /**
     * 获取学校详细信息
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busiSchoolsService.selectBusiSchoolsById(id));
    }

    /**
     * 新增学校
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:add')")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiSchools busiSchools)
    {
        return toAjax(busiSchoolsService.insertBusiSchools(busiSchools));
    }

    /**
     * 修改学校
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:edit')")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiSchools busiSchools)
    {
        return toAjax(busiSchoolsService.updateBusiSchools(busiSchools));
    }

    /**
     * 删除学校
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiSchools:remove')")
    @Log(title = "学校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busiSchoolsService.deleteBusiSchoolsByIds(ids));
    }
}
