package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ctop.entity.DramaInfo;
import org.jeecg.modules.ctop.entity.DramaItemInfo;
import org.jeecg.modules.ctop.entity.DramaRoleInfo;
import org.jeecg.modules.ctop.service.IDramaInfoService;
import org.jeecg.modules.ctop.service.IDramaItemInfoService;
import org.jeecg.modules.ctop.service.IDramaRoleInfoService;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 剧本信息
 * @author jeecg-boot
 * @date   2020-03-04
 * @version V1.0
 */
@Slf4j
@Api(tags="剧本信息")
@RestController
@RequestMapping("/ctop/dramaInfo")
public class DramaInfoController {
	@Autowired
	private IDramaInfoService dramaInfoService;
	@Autowired
	private IDramaItemInfoService dramaItemInfoService;
	@Autowired
	private IDramaRoleInfoService dramaRoleInfoService;
	@Autowired
	private ISysCategoryService categoryService;

	@GetMapping("/detail")
	public Map<String, Object> getDetail(Long dramaId) {
		Map<String, Object> result = new HashMap<>();
		List<DramaItemInfo> itemInfos = dramaItemInfoService.getByDramaId(dramaId);
        List<DramaItemInfo> getItemInfos = new ArrayList<>();
        for (DramaItemInfo itemInfo : itemInfos) {
            if (itemInfo.getType() == 2) {
                DramaRoleInfo roleInfo = dramaRoleInfoService.getById(itemInfo.getRoleId());
                itemInfo.setRoleName(roleInfo.getName());
            }
            getItemInfos.add(itemInfo);
        }
		List<DramaRoleInfo> roleInfos = dramaRoleInfoService.getByDramaId(dramaId);
        result.put("itemInfos", getItemInfos);
        result.put("roleInfos", roleInfos);
		ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
		return result;
	}
	/**
	  * 分页列表查询
	 * @param dramaInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "剧本信息-分页列表查询")
	@ApiOperation(value="剧本信息-分页列表查询", notes="剧本信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DramaInfo>> queryPageList(DramaInfo dramaInfo,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
        Result<IPage<DramaInfo>> result = new Result<>();
		QueryWrapper<DramaInfo> queryWrapper = QueryGenerator.initQueryWrapper(dramaInfo, req.getParameterMap());
        Page<DramaInfo> page = new Page<>(pageNo, pageSize);
		IPage<DramaInfo> pageList = dramaInfoService.page(page, queryWrapper);
        if (pageList != null && pageList.getRecords() != null && !pageList.getRecords().isEmpty()) {
			List<DramaInfo> list = new ArrayList<>();
			for (int i = 0; i < pageList.getRecords().size(); i++) {
				DramaInfo getDramaInfo = pageList.getRecords().get(i);
				SysCategory sysCategory = categoryService.getById(getDramaInfo.getCategory());
				if (sysCategory != null) {
					getDramaInfo.setCategory(sysCategory.getName());
				}
				list.add(getDramaInfo);
			}
			pageList.setRecords(list);
		}
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	  *   添加
	 * @param dramaInfo
	 * @return
	 */
	@AutoLog(value = "剧本信息-添加")
	@ApiOperation(value="剧本信息-添加", notes="剧本信息-添加")
	@PostMapping(value = "/add")
	public Result<DramaInfo> add(@RequestBody DramaInfo dramaInfo) {
		Result<DramaInfo> result = new Result<>();
		try {
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			dramaInfo.setUserId(sysUser.getId());
			dramaInfo.setUserName(sysUser.getUsername());
			dramaInfo.setUpdateTime(new Date());
			dramaInfoService.save(dramaInfo);

			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	  *  编辑
	 * @param dramaInfo
	 * @return
	 */
	@AutoLog(value = "剧本信息-编辑")
	@ApiOperation(value="剧本信息-编辑", notes="剧本信息-编辑")
	@PostMapping(value = "/edit")
	public Result<DramaInfo> edit(@RequestBody DramaInfo dramaInfo) {
        Result<DramaInfo> result = new Result<>();
		DramaInfo dramaInfoEntity = dramaInfoService.getById(dramaInfo.getId());
		if(dramaInfoEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = dramaInfoService.updateById(dramaInfo);
			if(ok) {
				result.success("修改成功!");
			}
		}

		return result;
	}

	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "剧本信息-通过id删除")
	@ApiOperation(value="剧本信息-通过id删除", notes="剧本信息-通过id删除")
	@DeleteMapping(value = "/delete")
    public Map<String, Object> delete(@RequestParam(name = "id", required = true) String id) {
        return dramaInfoService.delete(id);
	}

	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "剧本信息-批量删除")
	@ApiOperation(value="剧本信息-批量删除", notes="剧本信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<DramaInfo> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        Result<DramaInfo> result = new Result<>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.dramaInfoService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@AutoLog(value = "剧本信息-通过id查询")
	@ApiOperation(value="剧本信息-通过id查询", notes="剧本信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DramaInfo> queryById(@RequestParam(name="id",required=true) String id) {
        Result<DramaInfo> result = new Result<>();
		DramaInfo dramaInfo = dramaInfoService.getById(id);
		if(dramaInfo==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(dramaInfo);
			result.setSuccess(true);
		}
		return result;
	}
}
