package com.kiisoo.ic.system.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.system.bean.AccountBean;
import com.kiisoo.ic.system.bean.ImportAccountErrorBean;
import com.kiisoo.ic.system.bean.ModifyAccountInput;
import com.kiisoo.ic.system.bean.SaveAccountInput;
import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import com.kiisoo.ic.system.entity.PrivilageUserDO;
import com.kiisoo.ic.system.enums.AccountEnum;
import com.kiisoo.ic.system.service.PrivilageAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 账号  前端控制器
 * @author jinchaofan
 * @since 2020-02-19
 */
@Slf4j
@RestController
@RequestMapping("/account")
public class PrivilageAccountController extends BaseController {

    private final PrivilageAccountService privilageAccountService;

    @Autowired
    public PrivilageAccountController(PrivilageAccountService privilageAccountService) {
        this.privilageAccountService = privilageAccountService;
    }

    /**
     * 查询账号信息
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @param searchContent 查询内容
     * @param roleId 角色id
     * @param shopId 店铺id
     * @return 请求状态码，账号信息集合
     */
    @ResponseBody
    @PostMapping("/info/list")
    public Map<String, Object> listAccountInfoList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                   @RequestParam(value = "pageSize", required = false, defaultValue = "12") int pageSize,
                                                   @RequestParam(value = "searchContent", required = false) String searchContent,
                                                   @RequestParam(value = "roleId", required = false) Long roleId,
                                                   @RequestParam(value = "shopId", required = false) Long shopId) {
        try {
            IPage<AccountBean> accountBeanIPage = privilageAccountService.listAccountInfo(pageNum, pageSize, searchContent,
                    roleId, shopId);
            return data(accountBeanIPage);
        } catch (Exception e) {
           log.error("查询账号信息出错", e);
           return fail();
        }
    }

    /**
     *  账号管理-保存账号
     * @param addFrom 账号输入
     * @return 成功0000，失败0001，已存在0002
     */
    @ResponseBody
    @PostMapping("/save")
    public Map<String, Object> saveAccountInput(@RequestParam("addFrom")String addFrom) {
        try {
            JSONObject saveAccountInputJSON = JSONObject.parseObject(addFrom);
            SaveAccountInput saveAccountInput1 = saveAccountInputJSON.toJavaObject(SaveAccountInput.class);
            boolean flag = privilageAccountService.saveAccount(saveAccountInput1);
            //成功为0000，存在业务不能新增为0002
            return flag ? data(Boolean.TRUE) : fail(0);
        } catch (Exception e) {
            log.error("账号管理-保存账号出错", e);
            return fail();
        }

    }

    /**
     * 删除账号
     * @param accountId 账号id
     * @param userId 用户id
     * @return 是否成功
     */
    @ResponseBody
    @PostMapping("/remove")
    public Map<String, Object> removeAccout(@RequestParam("accountId") Long accountId,
                                            @RequestParam("userId") Long userId) {
        try {
            boolean flag = privilageAccountService.removeAccout(accountId, userId);
            if(flag){
                return success();
            }else {
                return fail();
            }
        } catch (Exception e) {
            log.error("删除账号出错", e);
            return fail();
        }
    }

    /**
     * 修改账号
     * @param modifyForm 修改账号输入类
     * @return 标识码
     */
    @ResponseBody
    @PostMapping("/modify")
    public Map<String, Object> modifyAccount(@RequestParam("modifyForm") String modifyForm) {
        try {
            JSONObject modifyJson = JSONObject.parseObject(modifyForm);
            ModifyAccountInput modifyAccountInput = modifyJson.toJavaObject(ModifyAccountInput.class);
            int code = privilageAccountService.modifyAccount(modifyAccountInput);
            if(code == AccountEnum.ACCOUNT_MODIFY_STATUS_EXISTS.getType()) {
                return fail(AccountEnum.ACCOUNT_MODIFY_STATUS_EXISTS.getType());
            }
            //成功
            return data(Boolean.TRUE);
        } catch (Exception e) {
            log.error("修改账号出错", e);
            return fail();
        }
    }

    /**
     * 重置密码
     * @param accountId 账号
     * @return 标识码
     */
    @ResponseBody
    @PostMapping("/reset/password")
    public Map<String, Object> resetPassword(@RequestParam("accountId") Long accountId) {
        try {
            int result = privilageAccountService.resetPassword(accountId);
            if(result >0) {
                return data(Boolean.TRUE);
            }
            return fail();
        } catch (Exception e) {
            log.error("重置密码出错", e);
            return fail();
        }
    }
    /**
     * 姓名修改
     * @param privilageUserDO 账号
     * @return 标识码
     */
    @ResponseBody
    @PostMapping("/name/modify")
    public Map<String, Object> modifyName(PrivilageUserDO privilageUserDO) {
        try {
            int result = privilageAccountService.modifyUser(privilageUserDO);
            if(result >0) {
                return data(Boolean.TRUE);
            }
            return fail();
        } catch (Exception e) {
            log.error("修改出错", e);
            return fail();
        }
    }

    /**
     * 修改账号状态
     * @param accountId
     * @param status
     * @return 标识码
     */
    @ResponseBody
    @PostMapping("/modify/status")
    public Map<String, Object> modifyAccountStatus(@RequestParam("accountId") Long accountId, @RequestParam("status") Integer status) {
        try {
            Integer result = privilageAccountService.modifyAccountStatusById(accountId,status);
            if(result>0){
                return data(Boolean.TRUE);
            }else{
                return fail();
            }
        } catch (Exception e) {
            log.error("修改账号状态出错", e);
            return fail();
        }
    }

    /**
     * 上传账户信息
     *
     * @param files  文件
     * @param userId 用户id
     * @return 成功或者失败原因
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json;charset=utf8")
    @ResponseBody
    public Map<String, Object> uploadShops(@RequestParam("file") MultipartFile[] files,
                                           @RequestParam("userId") Long userId) {
        try {
            List<ImportAccountErrorBean> errorBean = privilageAccountService.uploadAccount(files, userId);
            if(errorBean.isEmpty()){
                return success();
            }else {
                return data(errorBean);
            }
        } catch (Exception e) {
            log.error("上传失败", e);
            return fail();
        }
    }


}
