package com.ruoyi.web.controller.back;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.voluntary.domain.BusiAgents;
import com.ruoyi.voluntary.domain.BusiCardsOpen;
import com.ruoyi.voluntary.domain.BusiCardsOpenDetail;
import com.ruoyi.voluntary.domain.BusiSchools;
import com.ruoyi.voluntary.service.IBusiAgentsService;
import com.ruoyi.voluntary.service.IBusiCardsOpenDetailService;
import com.ruoyi.voluntary.service.IBusiCardsOpenService;
import com.ruoyi.voluntary.service.IBusiSchoolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 卡分配记录Controller
 * 
 * @author mingxue
 * @date 2021-11-18
 */
@RestController
@RequestMapping("/back/bBusiCardsOpen")
public class BackBusiCardsOpenController extends BaseController
{
    @Autowired
    private IBusiCardsOpenService cardsOpenService;
    @Autowired
    private IBusiCardsOpenDetailService cardsOpenDetailService;
    @Autowired
    private IBusiAgentsService agentsService;
    @Autowired
    private IBusiSchoolsService schoolsService;

    /**
     * 查询卡分配记录列表
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiCardsOpen bBusiCardsOpen)
    {
        startPage();
        List<BusiCardsOpen> list = cardsOpenService.selectBusiCardsOpenList(bBusiCardsOpen);
        Map<String,BusiAgents> agentsMap= new HashMap<>();
        Map<Long,String> schoolMap = schoolsService.selectBusiSchoolsList(new BusiSchools()).stream().collect(Collectors.toMap(BusiSchools::getId,BusiSchools::getName));
        list.forEach(co->{
            if(agentsMap.containsKey(co.getSender())){
                co.setAgent(agentsMap.get(co.getSender()));
            }else {
                BusiAgents agent= agentsService.selectBusiAgentsByAgentId(Long.parseLong(co.getSender()));
                co.setAgent(agent);
                agentsMap.put(co.getSender(),agent);
            }
            BusiCardsOpenDetail query= new BusiCardsOpenDetail();
            query.setOpenId(co.getId());
            List<BusiCardsOpenDetail> detailList = cardsOpenDetailService.selectBusiCardsOpenDetailList(query);
            co.setCount(detailList.size());
            //处理学校
            if(null!=co.getSchoolId()&&schoolMap.containsKey(co.getSchoolId().longValue())){
                co.setSchoolName(schoolMap.get(co.getSchoolId().longValue()));
            }
        });

        return getDataTable(list);
    }

    /**
     * 导出卡分配记录列表
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:export')")
    @Log(title = "卡分配记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusiCardsOpen bBusiCardsOpen)
    {
        List<BusiCardsOpen> list = cardsOpenService.selectBusiCardsOpenList(bBusiCardsOpen);
        ExcelUtil<BusiCardsOpen> util = new ExcelUtil<BusiCardsOpen>(BusiCardsOpen.class);
        return util.exportExcel(list, "卡分配记录数据");
    }

    /**
     * 获取卡分配记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cardsOpenService.selectBusiCardsOpenById(id));
    }

    /**
     * 新增卡分配记录
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:add')")
    @Log(title = "卡分配记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiCardsOpen bBusiCardsOpen)
    {
        return toAjax(cardsOpenService.insertBusiCardsOpen(bBusiCardsOpen));
    }

    /**
     * 修改卡分配记录
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:edit')")
    @Log(title = "卡分配记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiCardsOpen bBusiCardsOpen)
    {
        return toAjax(cardsOpenService.updateBusiCardsOpen(bBusiCardsOpen));
    }

    /**
     * 删除卡分配记录
     */
    @PreAuthorize("@ss.hasPermi('back:bBusiCardsOpen:remove')")
    @Log(title = "卡分配记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cardsOpenService.deleteBusiCardsOpenByIds(ids));
    }
}
