package com.ff.weijinxiao.controller;


import com.ff.weijinxiao.domain.CUser;
import com.ff.weijinxiao.domain.ExpendType;
import com.ff.weijinxiao.domain.UserInfo;
import com.ff.weijinxiao.jwt.JwtHelper;
import com.ff.weijinxiao.service.CUserService;
import com.ff.weijinxiao.service.ExpendTypeService;
import com.ff.weijinxiao.util.JsonTool;
import com.ff.weijinxiao.util.RandomUtil;
import com.ff.weijinxiao.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支出类型接口
 */
@RestController
@RequestMapping("api/expendtype")
@Api(value = "支出类型controller", tags = {"支出类型接口"})
public class ExpendTypeController {

    @Resource
    private ExpendTypeService expendTypeService;

    @Resource
    private CUserService cUserService;

    public UserInfo getUserInfoByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String openId = JwtHelper.parseToken(token);
        if (StringUtils.isBlank(openId)) {
            return null;
        }

        CUser cUser = cUserService.findByOpenId(openId);
        if (null == cUser) {
            return null;
        }
        return cUser.getUserInfo();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新支出类型信息", notes = "更新支出类型信息")
    public Map<String, Object> updateExpendType(@RequestBody ExpendType expendType, HttpServletRequest request) {
        try {
            expendTypeService.saveExpendType(expendType);
            return ResultTool.genSuccessResult("修改成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "修改失败:" + e.getMessage());
        }
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建支出类型信息", notes = "创建支出类型信息")
    public Map<String, Object> saveExpendType(@RequestBody ExpendType expendType, HttpServletRequest request) {
        try {
            expendTypeService.saveExpendType(expendType);
            return ResultTool.genSuccessResult("创建成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "创建失败:" + e.getMessage());
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除支出类型信息", notes = "删除支出类型信息")
    public Map<String, Object> deleteExpendType(@ApiParam(required = true, value = "支出类型id") @RequestBody String jsonId) {
        Integer id = JsonTool.parseJsonIdStr(jsonId);
        if (id < 0) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "id参数错误");
        }
        ExpendType expendType = expendTypeService.findById(id);
        if (null == expendType) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "支出类型不存在");
        }
        expendType.setIsDelete(1);
        expendTypeService.saveExpendType(expendType);
        return ResultTool.genSuccessResult("删除成功");
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询支出类型信息", notes = "根据id查询支出类型信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "支出类型id") @RequestParam String id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        try {
            ExpendType expendType = expendTypeService.findById(Integer.valueOf(id));
            if (null != expendType) {
                result.put("data", expendType);
                result.put("message", "查询成功");
                result.put("code", ResultTool.SuccessCode);
            } else {
                result.put("message", "查询失败，没有结果");
                result.put("code", ResultTool.ErrorCode_1);
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.put("message", "查询失败" + e.getMessage());
            result.put("code", ResultTool.ErrorCode_2);
        }

        return result;
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询支出类型列表", notes = "查询支出类型列表")
    public Map<String, Object> findList(@ApiParam(required = false, value = "支出类型名字") @RequestParam(required = false) String phone,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        param.put("EQ_isDelete", 0);
        if (null != phone && phone.length() > 0) {
            param.put("LIKE_phone", phone);
        }
        List<ExpendType> list = expendTypeService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询支出类型分页列表", notes = "查询支出类型分页列表")
    public Map<String, Object> findListPage(@ApiParam(required = false, value = "支出类型电话") @RequestParam(required = false) String phone,
                                            @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                            @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        param.put("EQ_isDelete", 0);
        if (null != phone && phone.length() > 0) {
            param.put("LIKE_phone", phone);
        }
        Page<ExpendType> list = expendTypeService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            result.put("data", list);
            result.put("message", "分页查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

    @PostMapping("/wxapi/create")
    @ApiOperation(value = "创建支出类型", notes = "创建支出类型")
    public Map<String, Object> createExpendType(@RequestBody ExpendType expendType, HttpServletRequest request) {
        try {
            expendType.setOnlyID(RandomUtil.genOnlyId());
            UserInfo userInfo = getUserInfoByToken(request);
            if (null != userInfo) {
                expendType.setUid(userInfo.getUid());
            }
            expendTypeService.saveExpendType(expendType);
            return ResultTool.genSuccessResult("创建支出类型成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "创建支出类型失败:" + e.getMessage());
        }
    }

    private String iconUrl = "http://test.platomix.net/wjxTest/images/m/";

    @GetMapping("/wxapi/list")
    @ApiOperation(value = "查询支出类型列表", notes = "查询支出类型列表")
    public Map<String, Object> wxfindList(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        UserInfo userInfo = getUserInfoByToken(request);
        if (null == userInfo) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "用户登录信息失败");
        }
        param.put("GT_id", 0);
        param.put("EQ_uid", userInfo.getUid());
        param.put("EQ_isDelete", 0);
        List<ExpendType> list = expendTypeService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            Map<String, Object> subParam = new HashMap<>();
            subParam.put("GT_id", 0);
            subParam.put("EQ_uid", userInfo.getUid());
            List<ExpendType> list1 = expendTypeService.findList(param);
            if (null == list1 || (null != list1 && list1.size() == 0)) {
                //默认创建支出类型给客户
                list = new ArrayList<>();
                String uid = userInfo.getUid();
                createExpendType(uid, "学习", "xuexi-2.png");
                createExpendType(uid, "书籍", "shuji.png");
                createExpendType(uid, "办公", "bangong.png");
                createExpendType(uid, "数码", "shuma.png");
                createExpendType(uid, "蔬菜", "shucai.png");
                createExpendType(uid, "购物", "gouwu.png");
                createExpendType(uid, "餐饮", "canyin.png");
                createExpendType(uid, "彩票", "caipiao.png");
                createExpendType(uid, "礼物", "liwu.png");
                createExpendType(uid, "通讯", "tongxun.png");
                createExpendType(uid, "零食", "lingshi.png");
                createExpendType(uid, "日用品", "riyongpin.png");
                createExpendType(uid, "长辈", "changbei.png");
                createExpendType(uid, "住房", "zhufang.png");
                createExpendType(uid, "旅行", "lvhang.png");
                createExpendType(uid, "服饰", "fushi.png");
                createExpendType(uid, "孩子", "haizi.png");
                createExpendType(uid, "捐赠", "juanzeng.png");
                createExpendType(uid, "礼金", "lijin.png");
                createExpendType(uid, "社交", "shejiao.png");
                createExpendType(uid, "交通", "traffic.png");
                createExpendType(uid, "水果", "shuiguo.png");
                createExpendType(uid, "医疗", "yiliao.png");
                createExpendType(uid, "娱乐", "yule.png");
                createExpendType(uid, "运动", "yundong.png");
                createExpendType(uid, "住房", "zhufang.png");
                createExpendType(uid, "快递", "kuaidi.png");
                list1 = expendTypeService.findList(param);
                result.put("data", list1);
                result.put("total", list1.size());
                result.put("message", "查询成功");
                result.put("code", 10000);
            } else {
                result.put("message", "没有数据，请先设置");
                result.put("code", 10001);
            }
        }

        return result;
    }

    @GetMapping("/wxapi/listall")
    @ApiOperation(value = "查询支出类型列表", notes = "查询支出类型列表")
    public Map<String, Object> wxfindListAll(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        UserInfo userInfo = getUserInfoByToken(request);
        if (null == userInfo) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "用户登录信息失败");
        }
        param.put("GT_id", 0);
        param.put("EQ_uid", userInfo.getUid());
        List<ExpendType> list = expendTypeService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
            return result;
        } else {
            //默认创建支出类型给客户
            list = new ArrayList<>();
            String uid = userInfo.getUid();
            createExpendType(uid, "学习", "xuexi-2.png");
            createExpendType(uid, "书籍", "shuji.png");
            createExpendType(uid, "办公", "bangong.png");
            createExpendType(uid, "数码", "shuma.png");
            createExpendType(uid, "蔬菜", "shucai.png");
            createExpendType(uid, "购物", "gouwu.png");
            createExpendType(uid, "餐饮", "canyin.png");
            createExpendType(uid, "彩票", "caipiao.png");
            createExpendType(uid, "礼物", "liwu.png");
            createExpendType(uid, "通讯", "tongxun.png");
            createExpendType(uid, "零食", "lingshi.png");
            createExpendType(uid, "日用品", "riyongpin.png");
            createExpendType(uid, "长辈", "changbei.png");
            createExpendType(uid, "住房", "zhufang.png");
            createExpendType(uid, "旅行", "lvhang.png");
            createExpendType(uid, "服饰", "fushi.png");
            createExpendType(uid, "孩子", "haizi.png");
            createExpendType(uid, "捐赠", "juanzeng.png");
            createExpendType(uid, "礼金", "lijin.png");
            createExpendType(uid, "社交", "shejiao.png");
            createExpendType(uid, "交通", "traffic.png");
            createExpendType(uid, "水果", "shuiguo.png");
            createExpendType(uid, "医疗", "yiliao.png");
            createExpendType(uid, "娱乐", "yule.png");
            createExpendType(uid, "运动", "yundong.png");
            createExpendType(uid, "住房", "zhufang.png");
            createExpendType(uid, "快递", "kuaidi.png");
            list = expendTypeService.findList(param);
        }
        result.put("data", list);
        result.put("total", list.size());
        result.put("message", "查询成功");
        result.put("code", 10000);
        return result;
    }

    void createExpendType(String uid, String name, String icon) {
        ExpendType expendType = new ExpendType();
        expendType.setUid(uid);
        expendType.setName(name);
        expendType.setIcon(iconUrl + icon);
        expendType.setOnlyID(RandomUtil.genOnlyIdNum());
        expendTypeService.saveExpendType(expendType);
    }

    @PostMapping("/wxapi/delete")
    @ApiOperation(value = "删除支出类型信息", notes = "删除支出类型信息")
    public Map<String, Object> wxdeleteExpendType(@ApiParam(required = true, value = "支出类型id") @RequestParam Integer id) {
        ExpendType expendType = expendTypeService.findById(id);
        if (null == expendType) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "支出类型不存在");
        }
        expendType.setIsDelete(1);
        expendTypeService.saveExpendType(expendType);
        return ResultTool.genSuccessResult("删除成功");
    }

    @PostMapping("/wxapi/undelete")
    @ApiOperation(value = "恢复已删除支出类型信息", notes = "恢复已删除支出类型信息")
    public Map<String, Object> wxUndeleteExpendType(@ApiParam(required = true, value = "支出类型id") @RequestParam Integer id) {
        ExpendType expendType = expendTypeService.findById(id);
        if (null == expendType) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "支出类型不存在");
        }
        expendType.setIsDelete(0);
        expendTypeService.saveExpendType(expendType);
        return ResultTool.genSuccessResult("删除成功");
    }
}
