package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
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.LifeUser;
import shop.alien.entity.store.vo.LifeFansVo;
import shop.alien.entity.store.vo.LifeStoreVo;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.mapper.StoreInfoMapper;
import shop.alien.store.service.LifeStoreService;
import shop.alien.util.common.FileUpload;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 门店
 */
@Api(tags = {"一期-门店"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/stores")
@RequiredArgsConstructor
public class LifeStoreController {

    private final LifeStoreService lifeStoreService;

    private final FileUpload fileUpload;

    private final StoreInfoMapper storeInfoMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation("商家登录")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "登录手机号码", dataType = "String", paramType = "query", required = true)})
    @PostMapping("/userLogin")
    public Object loginUser(@RequestParam("phone") String phone) {
        log.info("LifeStoreController.loginUser?phone={}", phone);
        LifeUser user = lifeStoreService.loginByPhone(phone);
        if (!ObjectUtils.isEmpty(user)) {
            return user;
        }
        return "该手机号没有注册账号";
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("我的关注")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "筛选：不传或0=全部关注；1且fansId为店铺时仅店铺关注", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "myFansId", value = "可选；有值时关注/拉黑四字段相对该 id，列表仍按 fansId", dataType = "String", paramType = "query")})
    @GetMapping("/getMyFollowed")
    public R<IPage<LifeFansVo>> getMyFollowed(@RequestParam("fansId") String fansId, @RequestParam("name") String name, @RequestParam("page") int page, @RequestParam("size") int size,
                                              @RequestParam(required = false) Integer type,
                                              @RequestParam(required = false) String myFansId) {
        log.info("LifeStoreController.getMyFollowed?fansId={},name={},page={},size={},type={},myFansId={}", fansId, name, page, size, type, myFansId);
        return R.data(lifeStoreService.getMyFollowed(fansId, name, page, size, type, myFansId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("我的粉丝")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "筛选：不传或0=全部粉丝；1且fansId为店铺时仅店铺粉丝", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "myFansId", value = "可选；有值时关注/拉黑四字段相对该 id，列表仍按 fansId", dataType = "String", paramType = "query")})
    @GetMapping("/getMyFans")
    public R<IPage<LifeFansVo>> getMyFans(String fansId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size,
                                          @RequestParam(required = false) Integer type,
                                          @RequestParam(required = false) String myFansId) {
        log.info("LifeStoreController.getMyFans?fansId={},name={},page={},size={},type={},myFansId={}", fansId, name, page, size, type, myFansId);
        return R.data(lifeStoreService.getMyFans(fansId, name, page, size, type, myFansId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("我的商家粉丝")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query")})
    @GetMapping("/getMyStoreFans")
    public R<IPage<LifeFansVo>> getMyStoreFans(String fansId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getMyStoreFans?fansId={},name={},page={},size={}", fansId, name, page, size);
        return R.data(lifeStoreService.getMyStoreFans(fansId, name, page, size));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("我的用户粉丝")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query")})
    @GetMapping("/getMyUserFans")
    public R<IPage<LifeFansVo>> getMyUserFans(String fansId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getMyUserFans?fansId={},name={},page={},size={}", fansId, name, page, size);
        return R.data(lifeStoreService.getMyUserFans(fansId, name, page, size));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("互相关注/好友")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query")})
    @GetMapping("/getMutualAttention")
    public R<IPage<LifeFansVo>> getMutualAttention(String fansId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getMutualAttention?fansId={},name={},page={},size={}", fansId, name, page, size);
        return R.data(lifeStoreService.getMutualAttention(fansId, name, page, size));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("互相关注/好友（仅用户）")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "fansId", value = "粉丝id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query")})
    @GetMapping("/getMutualAttentionUser")
    public R<IPage<LifeFansVo>> getMutualAttentionUser(String fansId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getMutualAttentionUser?fansId={},name={},page={},size={}", fansId, name, page, size);
        return R.data(lifeStoreService.getMutualAttentionUser(fansId, name, page, size));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("首页信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "phoneId", value = "登录id", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getHomePageInfo")
    public R<LifeFansVo> getHomePageInfo(String phoneId) {
        log.info("LifeStoreController.getHomePageInfo?phoneId={}", phoneId);
        return R.data(lifeStoreService.getHomePageInfo(phoneId));
    }

    @PostMapping("storeLogin")
    public R loginStore(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        log.info("LifeStoreController.loginStore?phone={},password={}", phone, password);
        String token = UUID.randomUUID().toString();
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b.phone", phone).eq("b.password", password).eq("a.delete_flag", 0);
        StoreInfoVo storeInfoVoOne = storeInfoMapper.getStoreInfoVoOne(queryWrapper);
        if (!ObjectUtils.isEmpty(storeInfoVoOne)) {
            storeInfoVoOne.setToken(token);
            return R.data(storeInfoVoOne);
        }
        return R.fail("用户名或密码错误");
    }

    @PostMapping("modifyStorePassword")
    public String modifyPassword(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        log.info("LifeStoreController.modifyPassword?phone={},password={}", phone, password);
        if (lifeStoreService.modifyPassword(phone, password)) {
            return "密码已经修改";
        } else {
            return "该手机号没有注册账号";
        }
    }

    @GetMapping("/getStores")
    public List<LifeStoreVo> getStores(@RequestParam(required = false) String userId, @RequestParam(required = false) String storeType, @RequestParam(required = false) String storeName, @RequestParam(required = false) String storeContact, @RequestParam(required = false) String storePhone) {
        log.info("LifeStoreController.getStores?userId={},storeType={},storeName={},storeContact={},storePhone={}", userId, storeType, storeName, storeContact, storePhone);
        return lifeStoreService.getStores(storeName, storeContact, storePhone, storeType, userId);
    }

    @ApiOperation("用户粉丝")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getUserFans")
    public R<IPage<LifeUserVo>> getUserFans(String storeId, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getUserFans?storeId={},name={},page={},size={}", storeId, name, page, size);
        return R.data(lifeStoreService.getUserFans(storeId, name, page, size));
    }

    @ApiOperation("商户粉丝")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeContact", value = "storeContact", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getStoreFans")
    public R<IPage<LifeStoreVo>> getStoreFans(String storeId,
                                              @RequestParam(defaultValue = "") String storeContact,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "10") int size) {
        log.info("LifeStoreController.getStoreFans?storeId={},storeContact={},page={},size={}", storeId, storeContact, page, size);
        return R.data(lifeStoreService.getStoreFans(storeId, storeContact, page, size));
    }

    @GetMapping("/{id}")
    public R<StoreInfoVo> getOneById(@PathVariable String id) {
        log.info("LifeStoreController.getOneById?id={}", id);
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.id", id).eq("a.delete_flag", 0);
        return R.data(storeInfoMapper.getStoreInfoVoOne(queryWrapper));
    }

    @PostMapping("/deleteLunbotu")
    public R<Boolean> deleteLunbotu(@RequestParam("id") String id) {
        log.info("LifeStoreController.deleteLunbotu?id={}", id);
        if (lifeStoreService.deleteLunbotu(id)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @GetMapping("getLunbotu")
    public List<String> getLunbotu() {
        log.info("LifeStoreController.getLunbotu");
        return lifeStoreService.getLunbotu();
    }

    @PostMapping("uploadLunbotu")
    public Map<String, Object> addStore(@RequestParam("id") String id, @RequestParam("image1") MultipartFile image1) {
        log.info("LifeStoreController.uploadLunbotu?id={}&image1={}", id, image1.getOriginalFilename());
        Map<String, Object> response = new HashMap<>();
        try {
            String path = "";
            if (!StringUtils.isEmpty(image1.getOriginalFilename())) {
                path = fileUpload.saveImage(image1);
            }
            response.put("success", lifeStoreService.uploadLunbotu(id, path));
        } catch (Exception e) {
            log.error("LifeStoreController.uploadLunbotu ERROR Msg={}", e.getMessage());
            response.put("success", "上传失败");
            return response;
        }
        return response;
    }

    @ApiOperation("商户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/selectStoreById")
    public R<StoreInfoVo> selectStoreById(@RequestParam("id") Integer id) {
        log.info("LifeStoreController.selectStoreById?id={}", id);
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.id", id).eq("a.delete_flag", 0);
        return R.data(storeInfoMapper.getStoreInfoVoOne(queryWrapper));
    }
}
