package com.huimv.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.UploadImage;
import com.huimv.admin.entity.AccountGroup;
import com.huimv.admin.entity.SysAccountMultilevel;
import com.huimv.admin.service.IAccountGroupService;
import com.huimv.admin.service.ISysAccountMultilevelService;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/accountMultilevel")
public class SysAccountMultilevelController {
    @Autowired
    private ISysAccountMultilevelService accountMultilevelService;

    @Autowired
    private IAccountGroupService accountGroupService;
    @Autowired
    private UploadImage uploadImage;


    @PostMapping("/list")
    public Result list(@RequestBody Map map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");

        return accountMultilevelService.findUserAccount(searchStr,pageNum,pageSize,userId);

    }

    @PostMapping("/listSubordinate")
    public Result listSubordinate(@RequestBody Map map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");
        return accountMultilevelService.listSubordinate(searchStr,pageNum,pageSize,userId);
    }

    @PostMapping("/listSubordinateByFarm")
    public Result listSubordinateByFarm(@RequestBody Map map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        String farmId = (String)map.get("farmId");
        return accountMultilevelService.listSubordinateByFarm(farmId,userId);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysAccountMultilevel sysAccountMultilevel , HttpServletRequest request){

        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return accountMultilevelService.add(sysAccountMultilevel,userId);

    }

    @PostMapping("/update")
    public Result update(@RequestBody SysAccountMultilevel sysAccountMultilevel){
        accountMultilevelService.updateById(sysAccountMultilevel);

        return new Result(10000,"修改成功",true);

    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(name ="userIds") List<Integer> userIds){

        accountGroupService.remove(new QueryWrapper<AccountGroup>().in("account_id",userIds));
        accountMultilevelService.removeByIds(userIds);

        return new Result(10000,"删除成功",true);

    }

    @GetMapping("/setAccountStatus")
    public Result setAccountStatus(@RequestParam(name = "ids") List<Integer> ids, @RequestParam(name = "status") Integer status){
        /* 设置账号状态 */
        return accountMultilevelService.setAccountStatus(ids,status);
    }

    @GetMapping("/findOne")
    public Result findOne(Integer id){
        return new Result(ResultCode.SUCCESS, accountMultilevelService.getById(id));
    }


    @GetMapping("/getLastFarmId")
    public Result getLastFarmId(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  accountMultilevelService.getLastFarmId(userId);
    }

    @PostMapping("/editImg")
    @Transactional(rollbackFor = Exception.class)
    public Result editImg(HttpServletRequest httpServletRequest, @RequestParam("img") MultipartFile img) throws IOException {
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelService.getById(TokenSign.getMemberIdByJwtToken(httpServletRequest));
        if (uploadImage.getImageCom(img).equals("上传失败")) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result(10001,"图片上传失败",false);
        }
        sysAccountMultilevel.setImgUrl(uploadImage.getImageCom(img));
        accountMultilevelService.updateById(sysAccountMultilevel);
        return Result.SUCCESS();
    }

    @PostMapping("/updateUserName")
    public Result updateUserName(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelService.getById(TokenSign.getMemberIdByJwtToken(httpServletRequest));
        sysAccountMultilevel.setUserName(paramsMap.get("userName"));
        accountMultilevelService.updateById(sysAccountMultilevel);
        return Result.SUCCESS();
    }

    @PostMapping("/updatePaper")
    public Result updatePaper(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelService.getById(TokenSign.getMemberIdByJwtToken(httpServletRequest));
        sysAccountMultilevel.setPapersType(paramsMap.get("papersType"));
        sysAccountMultilevel.setPapersCode(paramsMap.get("papersCode"));
        accountMultilevelService.updateById(sysAccountMultilevel);
        return Result.SUCCESS();
    }

    @PostMapping("/updateUser")
    public Result updateUser(HttpServletRequest httpServletRequest,
                             @RequestParam(value = "img", required = false) MultipartFile img,
                             @RequestParam(value = "userName", required = false) String userName,
                             @RequestParam(value = "phone", required = false) String phone,
                             @RequestParam(value = "workPost", required = false) Integer workPost,
                             @RequestParam(value = "workLocation", required = false) String workLocation,
                             @RequestParam(value = "workLocationId", required = false) String workLocationId,
                             @RequestParam(value = "workName", required = false) String workName,
                             @RequestParam(value = "papersType", required = false) String papersType,
                             @RequestParam(value = "papersCode", required = false) String papersCode) throws IOException {
        return accountMultilevelService.updateUser(httpServletRequest,img,userName,phone,workPost,workLocation,workLocationId,workName,papersType,papersCode);
    }

    @PostMapping("/getDetail")
    public Result getDetail(HttpServletRequest httpServletRequest){
        return new Result(ResultCode.SUCCESS,accountMultilevelService.getById(TokenSign.getMemberIdByJwtToken(httpServletRequest)));
    }
}
