package com.ff.weijinxiao.controller;


import com.ff.weijinxiao.domain.CUser;
import com.ff.weijinxiao.domain.UserInfo;
import com.ff.weijinxiao.domain.UserSetting;
import com.ff.weijinxiao.jwt.JwtHelper;
import com.ff.weijinxiao.service.CUserService;
import com.ff.weijinxiao.service.UserInfoService;
import com.ff.weijinxiao.service.UserSettingService;
import com.ff.weijinxiao.util.JsonTool;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户设置接口
 */
@RestController
@RequestMapping("api/userSetting")
@Api(value = "用户设置controller", tags = {"用户设置接口"})
public class UserSettingController {

    @Resource
    private UserSettingService userSettingService;

    @Resource
    private UserInfoService userInfoService;

    @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> updateUserSetting(@RequestBody UserSetting userSetting, HttpServletRequest request) {
        try {
            userSettingService.saveUserSetting(userSetting);
            return ResultTool.genSuccessResult("修改成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "修改失败:" + e.getMessage());
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除用户设置信息", notes = "删除用户设置信息")
    public Map<String, Object> deleteUserSetting(@ApiParam(required = true, value = "用户设置id") @RequestBody String jsonId) {
        Integer id = JsonTool.parseJsonIdStr(jsonId);
        if (id < 0) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "id参数错误");
        }
        userSettingService.deleteById(id);
        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 {
            UserSetting userSetting = userSettingService.findById(Integer.valueOf(id));
            if (null != userSetting) {
                result.put("data", userSetting);
                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<UserSetting> list = userSettingService.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<UserSetting> list = userSettingService.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/setPurchaseSetting")
    @ApiOperation(value = "提交进货设置", notes = "提交进货设置")
    public Map<String, Object> setPurchaseSetting(@RequestBody UserSetting userSetting, HttpServletRequest request) {
        try {
            UserInfo userInfo = getUserInfoByToken(request);
            if (null == userInfo) {
                return ResultTool.genFailResult(ResultTool.ErrorCode_1, "操作失败，未注册有效账号");
            }
            UserSetting setting = userSettingService.getSettingByUid(userInfo.getUid());
            if (null != setting) {
                setting.setGoodsCategory(userSetting.getGoodsCategory());
                setting.setGoodsCode(userSetting.getGoodsCode());
                setting.setGoodsBarCode(userSetting.getGoodsBarCode());
                setting.setRealPayMoney(userSetting.getRealPayMoney());
                setting.setDebt(userSetting.getDebt());
                setting.setSupplier(userSetting.getSupplier());
                setting.setProductionDate(userSetting.getProductionDate());
                setting.setBeGoodTime(userSetting.getBeGoodTime());
                setting.setWarningDays(userSetting.getWarningDays());
                setting.setStockWarningAmount(userSetting.getStockWarningAmount());
                setting.setFactory(userSetting.getFactory());
                setting.setExplains(userSetting.getExplains());
                setting.setImage(userSetting.getImage());
                setting.setPrices2(userSetting.getPrices2());
                setting.setPrices3(userSetting.getPrices3());
                setting.setPrices4(userSetting.getPrices4());
                setting.setPrices5(userSetting.getPrices5());
                setting.setPurchaseDate(userSetting.getPurchaseDate());
                userSettingService.saveUserSetting(setting);
            } else {
                userSetting.setUid(userInfo.getUid());
                userSettingService.saveUserSetting(userSetting);
            }
            return ResultTool.genSuccessResult("提交用户设置成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "提交用户设置失败:" + e.getMessage());
        }
    }


    @PostMapping("/wxapi/setPayWay")
    @ApiOperation(value = "设置付款方式", notes = "设置付款方式")
    public Map<String, Object> setPayWay(@RequestBody UserSetting userSetting, HttpServletRequest request) {
        try {
            UserInfo userInfo = getUserInfoByToken(request);
            if (null == userInfo) {
                return ResultTool.genFailResult(ResultTool.ErrorCode_1, "操作失败，未注册有效账号");
            }
            UserSetting setting = userSettingService.getSettingByUid(userInfo.getUid());
            if (null != setting) {
                setting.setPayWay1(userSetting.getPayWay1());
                setting.setPayWay2(userSetting.getPayWay2());
                setting.setPayWay3(userSetting.getPayWay3());
                setting.setPayWay4(userSetting.getPayWay4());
                setting.setPayWay5(userSetting.getPayWay5());
                setting.setPayWay6(userSetting.getPayWay6());
                setting.setPayWay7(userSetting.getPayWay7());
                setting.setPayWay8(userSetting.getPayWay8());
                userSettingService.saveUserSetting(setting);
            } else {
                userSetting.setUid(userInfo.getUid());
                userSettingService.saveUserSetting(userSetting);
            }
            return ResultTool.genSuccessResult("提交用户设置成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "提交用户设置失败:" + e.getMessage());
        }
    }

    @PostMapping("/wxapi/saveSetting")
    @ApiOperation(value = "保存设置", notes = "保存设置")
    public Map<String, Object> saveSetting(@RequestBody UserSetting userSetting, HttpServletRequest request) {
        try {
            UserInfo userInfo = getUserInfoByToken(request);
            if (null == userInfo) {
                return ResultTool.genFailResult(ResultTool.ErrorCode_1, "操作失败，未注册有效账号");
            }
            UserSetting setting = userSettingService.getSettingByUid(userInfo.getUid());
            if (null != setting) {
                userSettingService.saveUserSetting(userSetting);
            } else {
                userSetting.setUid(userInfo.getUid());
                userSettingService.saveUserSetting(userSetting);
            }
            return ResultTool.genSuccessResult("提交用户设置成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "提交用户设置失败:" + e.getMessage());
        }
    }

    @GetMapping("/wxapi/getMySetting")
    @ApiOperation(value = "查询用户设置分页列表", notes = "查询用户设置分页列表")
    public Map<String, Object> wxfindListPage(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        UserInfo userInfo = getUserInfoByToken(request);
        if (null == userInfo) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "操作失败，未注册有效账号");
        }
        UserSetting setting = userSettingService.getSettingByUid(userInfo.getUid());
        if (null == setting) {
            setting = new UserSetting();
        }

        result.put("data", setting);
        result.put("message", "查询成功");
        result.put("code", 10000);

        return result;
    }

}
