package com.ruoyi.web.controller.back;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RoleType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
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.BusiAgents;
import com.ruoyi.voluntary.service.IBusiAgentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理商Controller
 *
 * @author voluntary
 * @date 2022-11-20
 */
@RestController
@RequestMapping("/voluntary/busiAgents")
public class BackBusiAgentsController extends BaseController
{
    @Autowired
    private IBusiAgentsService busiAgentsService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询代理商列表
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiAgents busiAgents)
    {
        startPage();
        List<BusiAgents> list = busiAgentsService.selectBusiAgentsList(busiAgents);
        return getDataTable(list);
    }

    @GetMapping("/getAllAgents")
    public TableDataInfo getAllAgents()
    {
        List<BusiAgents> list = busiAgentsService.selectBusiAgentsList(new BusiAgents());
        return getDataTable(list);
    }

    /**
     * 导出代理商列表
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:export')")
    @Log(title = "代理商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiAgents busiAgents)
    {
        List<BusiAgents> list = busiAgentsService.selectBusiAgentsList(busiAgents);
        ExcelUtil<BusiAgents> util = new ExcelUtil<BusiAgents>(BusiAgents.class);
        util.exportExcel(response, list, "代理商数据");
    }

    /**
     * 获取代理商详细信息
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:query')")
    @GetMapping(value = "/{agentId}")
    public AjaxResult getInfo(@PathVariable("agentId") Long agentId)
    {
        return success(busiAgentsService.selectBusiAgentsByAgentId(agentId));
    }

    /**
     * 新增代理商
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:add')")
    @Log(title = "代理商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiAgents busiAgents)
    {
        // 代理商新增时，先增加sysUser中的用户及sysUserRole中的角色，最后再保存代理商信息
        List<BusiAgents> agents = busiAgentsService.selectBusiAgentsList(new BusiAgents().setPhonenumber(busiAgents.getPhonenumber()));
        SysUser user00 = userService.selectUserByUserNameOrPhone(busiAgents.getPhonenumber());
        if(CollectionUtils.isNotEmpty(agents)||null!=user00){
            return AjaxResult.error("账号已存在，请更换");
        }
        SysUser insertUser = new SysUser();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        insertUser.setPassword(SecurityUtils.encryptPassword(password));
        insertUser.setPassword2(password);
        insertUser.setUserName(busiAgents.getPhonenumber());
        insertUser.setPhonenumber(busiAgents.getPhonenumber());
        insertUser.setNickName(busiAgents.getName());
        Long[] roleIds = new Long[]{RoleType.agent.getValue()};
        insertUser.setRoleIds(roleIds);
        userService.insertUser(insertUser);

        busiAgents.setUserId(insertUser.getUserId());
        return toAjax(busiAgentsService.insertBusiAgents(busiAgents));
    }

    /**
     * 修改代理商
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:edit')")
    @Log(title = "代理商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiAgents busiAgents)
    {
        return toAjax(busiAgentsService.updateBusiAgents(busiAgents));
    }

    /**
     * 删除代理商
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiAgents:remove')")
    @Log(title = "代理商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{agentIds}")
    public AjaxResult remove(@PathVariable Long[] agentIds)
    {
        return toAjax(busiAgentsService.deleteBusiAgentsByAgentIds(agentIds));
    }
}
