package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.manage.modules.actor.entity.*;
import cn.com.ctop.manage.modules.actor.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jeecg-boot
 */
@RestController
@Slf4j
@RequestMapping("/ctop/prop")
public class PropController {
    @Autowired
    private IPropService propService;
    @Autowired
    private IPropPhotoService propPhotoService;

    @ResponseBody
    @GetMapping("/detail")
    public Result<Prop> getDetail(@RequestParam(name = "propId") Long propId) {
        Result<Prop> result = new Result<>();
        Prop prop = propService.getById(propId);
        result.setResult(prop);
        result.setSuccess(true);
        return result;
    }

    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            propService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败",e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 分页列表查询
     *
     * @param prop
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<Prop>> queryPageList(Prop prop,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) {
        Result<IPage<Prop>> result = new Result<>();
        QueryWrapper<Prop> queryWrapper = QueryGenerator.initQueryWrapper(prop, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<Prop> page = new Page<>(pageNo, pageSize);
        IPage<Prop> pageList = propService.page(page, queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @GetMapping(value = "/photo/list")
    public Result<IPage<PropPhoto>> queryPhotoPageList(PropPhoto propPhoto,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<PropPhoto>> result = new Result<>();

        QueryWrapper<PropPhoto> queryWrapper = QueryGenerator.initQueryWrapper(propPhoto, req.getParameterMap());
        Page<PropPhoto> page = new Page<>(pageNo, pageSize);
        IPage<PropPhoto> pageList = propPhotoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @DeleteMapping(value = "/photo/delete")
    public Result<PropPhoto> deletePhoto(@RequestParam(name = "id", required = true) String id) {
        Result<PropPhoto> result = new Result<>();
        PropPhoto propPhoto = propPhotoService.getById(id);
        if (propPhoto == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = propPhotoService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    @PostMapping(value = "/photo/add")
    public Result<PropPhoto> addPhoto(PropPhoto propPhoto) {
        Result<PropPhoto> result = new Result<>();
        try {
            String photoUrl = propPhoto.getPhotoUrl();
            if (!Check.isNull(photoUrl) && !photoUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                propPhoto.setPhotoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + photoUrl);
            }
            propPhotoService.save(propPhoto);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 添加
     *
     * @param prop
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Prop> add(@RequestBody Prop prop) {
        Result<Prop> result = new Result<>();
        try {
            String coverUrl = prop.getCoverUrl();
            if (!Check.isNull(coverUrl) && !coverUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                prop.setCoverUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + coverUrl);
            }
            propService.save(prop);
            if (null != prop.getImageList() && !prop.getImageList().isEmpty()) {
                for (String url : prop.getImageList()) {
                    PropPhoto photo = new PropPhoto();
                    photo.setPropId(prop.getId());
                    if (!Check.isNull(url) && !url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                        photo.setPhotoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + url);
                    }
                    propPhotoService.save(photo);
                }
            }
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param prop
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<Prop> edit(@RequestBody Prop prop) {
        Result<Prop> result = new Result<>();

        Prop propEntity = propService.getById(prop.getId());
        if (propEntity == null) {
            result.error500("未找到对应实体");
        } else {
            String coverUrl = prop.getCoverUrl();
            if (!Check.isNull(coverUrl) && !coverUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                prop.setCoverUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + coverUrl);
            }

            boolean ok = propService.updateById(prop);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }
}
