package com.ruoyi.web.controller.api;


import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallOrderBack;
import com.ruoyi.mall.domain.MallOrderOrder;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.domain.vo.MallOrderGoodsVO;
import com.ruoyi.mall.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 业务员
 * @author chang
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/salesman")
@ApiOperation("业务员")
public class ApiSalesmanController {

    @Resource
    private IMallSalesmanService mallSalesmanService;

    @Autowired
    IMallOrderOrderService orderService;

    @Autowired
    IMallOrderBackService orderBackService;

    @Autowired
    IMallOrderGoodsService orderGoodsService;

    @Autowired
    IMallMemberUserService mallMemberUserService;

    /**
     * 业务员登录
     * @param param
     * @return
     */
    @PostMapping("/salesmanLogin")
    @ApiOperation("业务员登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String")
    })
    public ApiResult salesmanLogin(@RequestBody Map<String,Object> param){
        if (!param.containsKey("phone") || MapUtils.getString(param,"phone") == null){
            return ApiResult.error("缺少phone");
        }
        if (!param.containsKey("password") || MapUtils.getString(param,"password") == null){
            return ApiResult.error("缺少password");
        }
        try {
            return mallSalesmanService.salesmanLogin(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 业务员注销登录
     * @param param
     * @return
     */
    @PostMapping("/salesmanLogout")
    @ApiOperation("业务员注销登录")
    public ApiResult salesmanLogout(@RequestBody Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getString(param,"salesmanId") == null){
            return ApiResult.error("缺少salesmanId");
        }
        try {
            return mallSalesmanService.salesmanLogout(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 业务员个人信息
     * @param param
     * @return
     */
    @GetMapping("/getSalesmanInfo")
    @ApiOperation("业务员个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer")
    })
    public ApiResult getSalesmanInfo(@RequestParam Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null){
            return ApiResult.error("缺少salesmanId");
        }
        try {
            MallSalesman salesmanId = mallSalesmanService.getSalesmanInfo(MapUtils.getInteger(param, "salesmanId"));
            salesmanId.setPassword("");
            return ApiResult.ok().put("data",salesmanId);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 业务员修改头像名称
     * @param param
     * @return
     */
    @PostMapping("/salesmanEdit")
    @ApiOperation("业务员修改头像名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer"), @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String")
    })
    public ApiResult salesmanEdit(@RequestBody Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null){
            return ApiResult.error("缺少salesmanId");
        }
        if (!param.containsKey("avatar")){
            return ApiResult.error("缺少avatar");
        }
        if (!param.containsKey("name")){
            return ApiResult.error("缺少name");
        }
        try {
            return mallSalesmanService.salesmanEdit(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 业务员首页业绩
     * @param param
     * @return
     */
    @GetMapping("/indexData")
    @ApiOperation("业务员首页业绩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer"), @ApiImplicitParam(name = "type", value = "时间筛选（0-日，1-月，2-年）", dataType = "Integer")
    })
    public ApiResult indexData(@RequestParam Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null){
            return ApiResult.error("缺少salesmanId");
        }
        if (!param.containsKey("type") || MapUtils.getInteger(param,"type") == null){
            return ApiResult.error("缺少type");
        }
        try {
            return mallSalesmanService.indexData(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 客户列表
     * @param param
     * @return
     */
    @GetMapping("/salesmanCustomerList")
    @ApiOperation("客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer"), @ApiImplicitParam(name = "timeBegin", value = "时间区间开始", dataType = "String"),
            @ApiImplicitParam(name = "timeEnd", value = "时间区间结束", dataType = "String"), @ApiImplicitParam(name = "province", value = "省份", dataType = "String"),
            @ApiImplicitParam(name = "city", value = "城市", dataType = "String"), @ApiImplicitParam(name = "area", value = "区域", dataType = "String"),
            @ApiImplicitParam(name = "searchValue", value = "搜索值", dataType = "String"), @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示数量", dataType = "Integer")
    })
    public ApiResult salesmanCustomerList(@RequestParam Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null) {
            return ApiResult.error("缺少salesmanId");
        }
        if (!param.containsKey("pageNum")) {
            return ApiResult.error("缺少pageNum");
        }
        if (!param.containsKey("pageSize")) {
            return ApiResult.error("缺少pageSize");
        }

        try {
            return mallSalesmanService.salesmanCustomerList(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 新建客户
     * @param param
     * @return
     */
    @PostMapping("/createMemberUser")
    @ApiOperation("新建客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer"), @ApiImplicitParam(name = "name", value = "姓名", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String"), @ApiImplicitParam(name = "password", value = "密码", dataType = "String"),
            @ApiImplicitParam(name = "province", value = "省份", dataType = "String"), @ApiImplicitParam(name = "city", value = "城市", dataType = "String"),
            @ApiImplicitParam(name = "area", value = "区域", dataType = "String"), @ApiImplicitParam(name = "address", value = "地址", dataType = "String"),
            @ApiImplicitParam(name = "shopName", value = "店名", dataType = "String")
    })
    public ApiResult createMemberUser(@RequestBody Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null) {
            return ApiResult.error("缺少salesmanId");
        }
        if (!param.containsKey("name") || MapUtils.getString(param,"name") == null) {
            return ApiResult.error("缺少name");
        }
        if (!param.containsKey("phone") || MapUtils.getString(param,"phone") == null) {
            return ApiResult.error("缺少phone");
        }
        if (!param.containsKey("province") || MapUtils.getString(param,"province") == null)
            return ApiResult.error("缺少province");
        if (!param.containsKey("city") || MapUtils.getString(param,"city") == null) {
            return ApiResult.error("缺少city");
        }
        if (!param.containsKey("area") || MapUtils.getString(param,"area") == null) {
            return ApiResult.error("缺少area");
        }
        if (!param.containsKey("address") || MapUtils.getString(param,"address") == null) {
            return ApiResult.error("缺少address");
        }
        if (!param.containsKey("shopName") || MapUtils.getString(param,"shopName") == null) {
            return ApiResult.error("缺少shopName");
        }

        try {
            return mallSalesmanService.createMemberUser(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 业绩
     * @param param
     * @return
     */
    @GetMapping("/salesmanAchievement")
    @ApiOperation("客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer")
    })
    public ApiResult salesmanAchievement(@RequestParam Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null) {
            return ApiResult.error("缺少salesmanId");
        }

        try {
            return mallSalesmanService.salesmanAchievement(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 订单列表
     * @param param
     * @return
     */
    @GetMapping("/salesmanOrderList")
    @ApiOperation("订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salesmanId", value = "业务员id", dataType = "Integer"), @ApiImplicitParam(name = "type", value = "订单状态", dataType = "Integer"),
            @ApiImplicitParam(name = "timeBegin", value = "时间区间开始", dataType = "String"), @ApiImplicitParam(name = "timeEnd", value = "时间区间结束", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer"), @ApiImplicitParam(name = "pageSize", value = "每页展示数量", dataType = "Integer")
    })
    public ApiResult salesmanOrderList(@RequestParam Map<String,Object> param){
        if (!param.containsKey("salesmanId") || MapUtils.getInteger(param,"salesmanId") == null) {
            return ApiResult.error("缺少salesmanId");
        }
        if (!param.containsKey("type") || MapUtils.getInteger(param,"type") == null) {
            return ApiResult.error("缺少type");
        }
        if (!param.containsKey("pageNum")) {
            return ApiResult.error("缺少pageNum");
        }
        if (!param.containsKey("pageSize")){
            return ApiResult.error("缺少pageSize");
        }

        try {
            return mallSalesmanService.salesmanOrderList(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

    /**
     * 订单详情
     * @param param
     * @return
     */
    @GetMapping("/salesmanOrderDetail")
    @ApiOperation("订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "Integer")
    })
    public ApiResult salesmanOrderDetail(@RequestParam Map<String,Object> param){
        if (!param.containsKey("orderId")){
            return ApiResult.error("缺少orderId");
        }

        try {
            MallOrderOrder order = orderService.selectMallOrderOrderById(Long.parseLong(param.get("orderId").toString()));

            //用户信息
            MallMemberUser memberUser = mallMemberUserService.selectMallMemberUserById(order.getUserId());
            if (ObjectUtils.isNotEmpty(memberUser)){
                order.setMemberUserName(memberUser.getName());
                order.setMemberUserPhone(memberUser.getPhone());
            }

            List<MallOrderGoodsVO> orderGoodsVOList = orderGoodsService.selectMallOrderGoodsByOrderId(order.getId());
            order.setGoodsVOList(orderGoodsVOList);
            if ("5".equals(order.getStatus()) || "6".equals(order.getStatus())){
                List<MallOrderBack> list = orderBackService.selectMallOrderBackByOrderId(Long.parseLong(param.get("orderId").toString()));
                MallOrderBack result = new MallOrderBack();
                for (MallOrderBack orderBack: list) {
                    result = orderBack;
                }
                order.setOrderBack(result);
            }
            return ApiResult.ok().put("data", order);
        }catch (Exception e) {
            return ApiResult.ok("获取订单失败");
        }
    }
}
