package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeFans;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.mapper.LifeUserMapper;
import shop.alien.store.service.LifeUserService;
import shop.alien.util.common.FileUpload;
import shop.alien.util.common.RandomCreateUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@Api(tags = {"一期-用户"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class LifeUserController {

    private final LifeUserService service;

    private final FileUpload fileUpload;

    private final LifeUserMapper lifeUserMapper;

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "phoneNum", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/userLogin")
    public R<LifeUserVo> userLogin(@RequestParam("phoneNum") String phoneNum) {
        log.info("LifeUserController.userLogin?phoneNum={}", phoneNum);
        LifeUserVo userVo = service.userLogin(phoneNum);
        if (null == userVo) {
            return R.fail("登录失败");
        }
        return R.data(userVo);
    }

    @ApiOperation("查询用户信息")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/selectUserById")
    public R<LifeUser> selectUserById(@RequestParam("id") String id) {
        log.info("LifeUserController.selectUserById?id={}", id);
        LifeUser lifeUserVo = new LifeUser();
        LifeUser user = lifeUserMapper.selectById(id);
        if (null == user) {
            return R.fail("查询失败");
        }
//        BeanUtils.copyProperties(user, lifeUserVo);
//        // 添加 是否拉黑标识,是否是达人标识
//        //达人标识 0 不是达人 1 是达人
//        LambdaUpdateWrapper<LifeUserExpert> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.eq(LifeUserExpert::getUserId, user.getId());
//        LifeUserExpert lifeUserExpert = lifeUserExpertMapper.selectOne(wrapper);
//        if (null != lifeUserExpert) {
//            lifeUserVo.setIsExpert("1");
//        } else {
//            lifeUserVo.setIsExpert("0");
//        }
        // 被拉黑标识 0 未拉黑 1 已拉黑(待定)

        return R.data(user);
    }

    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 3)
    @PostMapping("/modifyUser")
    public R<Boolean> modifyUser(@RequestBody LifeUser user) {
        log.info("LifeUserController.modifyUser?user={}", user.toString());
        int num = service.modifyUser(user);
        if (num == 0) {
            return R.fail("修改失败");
        }
        return R.success("修改成功");
    }

    @ApiOperation("关注")
    @ApiOperationSupport(order = 4)
    @PostMapping("/addFans")
    public R<Boolean> addFans(@RequestBody LifeFans fans) {
        log.info("LifeUserController.addFans?fans={}", fans.toString());
        int num = service.addFans(fans);
        if (num == 0) {
            return R.fail("关注失败");
        }
        return R.success("关注成功");
    }

    @ApiOperation("取消关注")
    @ApiOperationSupport(order = 5)
    @PostMapping("/cancelFollewed")
    public R<Boolean> cancelFollewed(@RequestBody LifeFans fans) {
        log.info("LifeUserController.cancelFollewed?fans={}", fans.toString());
        int num = service.cancelFans(fans);
        if (num == 0) {
            return R.fail("取消失败");
        }
        return R.success("取消成功");
    }

    @ApiOperation("搜索商家或用户")
    @ApiOperationSupport(order = 6)
    @PostMapping("/getStoreAndUserByName")
    public R<IPage<LifeUserVo>> getStoreAndUserByName(@RequestBody LifeUserVo vo) {
        log.info("LifeUserController.getStoreAndUserByName?vo={}", vo.toString());
        return R.data(service.getStoreAndUserByName(vo));
    }

    @GetMapping("/getUserById")
    public LifeUser getUserById(@RequestParam("id") String id) {
        log.info("LifeUserController.getUserById?id={}", id);
        return service.getUserById(id);
    }

    @PostMapping("/addUser")
    public int addOrUpdateUser(LifeUser user, @RequestParam(value = "image", required = false) MultipartFile image) throws Exception {
        log.info("LifeUserController.addOrUpdateUser?user={}&image={}", user.toString(), image.getOriginalFilename());
        String path = "";
        if (!StringUtils.isEmpty(image.getOriginalFilename())) {
            path = fileUpload.saveImage(image);
        }
        // 设置图片路径
        user.setUserImage(path);
        //邀请码

        user.setInvitedNum(RandomCreateUtil.getRandomNStr(20));
        return service.addOrUpdateUser(user);
    }

    @GetMapping("/getUserPage")
    public Map<String, Object> getStoresPage(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int size,
                                             @RequestParam(required = false) String realName,
                                             @RequestParam(required = false) String userPhone) {
        log.info("LifeUserController.getStoresPage?page={},size={},realName={},userPhone={}", page, size, realName, userPhone);
        IPage<LifeUser> stores = service.getStoresPage(page, size, realName, userPhone);
        Map<String, Object> response = new HashMap<>();
        response.put("data", stores.getRecords());
        response.put("total", stores.getRecords().size());
        response.put("current", stores.getCurrent());
        response.put("size", stores.getSize());
        return response;
    }

    /**
     * 用户端注销用户
     */
    @ApiOperation("用户端注销用户")
    @PostMapping("/liftCancelAccount")
    public R<Boolean> liftCancelAccount(@RequestBody LifeUser user) {
        log.info("StoreUserController.liftCancelAccount?LifeUser={}", user);
        service.liftCancelAccount(user);
        return R.success("注销成功");
    }

    /**
     * 用户端撤回注销用户
     */
    @ApiOperation("用户端撤回注销用户")
    @PostMapping("/liftCancelAccountUn")
    public R<Boolean> liftCancelAccountUn(@RequestBody LifeUser user) {
        log.info("StoreUserController.liftCancelAccountUn?LifeUser={}", user);
        service.liftCancelAccountUn(user.getId());
        return R.success("撤回注销成功");
    }

    @GetMapping("/getUserByPhone")
    public R<LifeUser> getUserByPhone(@RequestParam("phone") String phone) {
        log.info("LifeUserController.getUserByPhone?phone={}", phone);
        return R.data(service.getUserByPhone(phone));
    }
}
